Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. if (fork() == 0) {
  2. ...
  3. cudaMalloc(....);
  4. ...
  5. }
  6.  
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <cuda_runtime.h>
  13.  
  14. #define PERR(call)
  15. if (call) {
  16. fprintf(stderr, "%s:%d Error [%s] on "#call"n", __FILE__, __LINE__,
  17. cudaGetErrorString(cudaGetLastError()));
  18. exit(1);
  19. }
  20.  
  21. int
  22. main(int argc, char **argv)
  23. {
  24. float *v_d;
  25. int gpucount;
  26.  
  27. cudaGetDeviceCount(&gpucount);
  28.  
  29. if (fork() == 0) {
  30. cudaSetDevice(0);
  31. PERR(cudaMalloc(&v_d, 1000*sizeof(float)));
  32. }
  33. wait(NULL);
  34. return 0;
  35. }
  36.  
  37. PROGS = fork
  38. CUDA_PATH = /usr/local/cuda
  39. CXXFLAGS = -g -O0 -Wall
  40. CXXINCLUDES = -I$(CUDA_PATH)/include
  41. NVCC := $(CUDA_PATH)/bin/nvcc -ccbin $(CXX) -Xcompiler "$(CXXFLAGS)"
  42.  
  43. fork: fork.cxx
  44. $(NVCC) $^ -o $@ $(LIBS)
  45.  
  46. clean:
  47. (rm $(PROGS) *.o)
  48.  
  49. if (fork() == 0) {
  50. PERR(cudaGetDeviceCount(&gpucount));
  51. return(gpucount);
  52. }
  53. wait(&gpucount);
  54. gpucount = WEXITSTATUS(gpucount);
  55.  
  56. $ cat t345.cu
  57. #include <unistd.h> /* Symbolic Constants */
  58. #include <sys/types.h> /* Primitive System Data Types */
  59. #include <errno.h> /* Errors */
  60. #include <stdio.h> /* Input/Output */
  61. #include <sys/wait.h> /* Wait for Process Termination */
  62. #include <stdlib.h> /* General Utilities */
  63.  
  64.  
  65. #define cudaCheckErrors(msg)
  66. do {
  67. cudaError_t __err = cudaGetLastError();
  68. if (__err != cudaSuccess) {
  69. fprintf(stderr, "Fatal error: %s (%s at %s:%d)n",
  70. msg, cudaGetErrorString(__err),
  71. __FILE__, __LINE__);
  72. fprintf(stderr, "*** FAILED - ABORTINGn");
  73. exit(1);
  74. }
  75. } while (0)
  76.  
  77.  
  78. __global__ void addkernel(int *data){
  79. *data += 1;
  80. }
  81.  
  82. int main()
  83. {
  84. pid_t childpid; /* variable to store the child's pid */
  85. int retval; /* child process: user-provided return code */
  86. int status; /* parent process: child's exit status */
  87.  
  88. /* only 1 int variable is needed because each process would have its
  89. own instance of the variable
  90. here, 2 int variables are used for clarity */
  91.  
  92. /* now create new process */
  93. childpid = fork();
  94.  
  95. if (childpid >= 0) /* fork succeeded */
  96. {
  97. if (childpid == 0) /* fork() returns 0 to the child process */
  98. {
  99. printf("CHILD: I am the child process!n");
  100. printf("CHILD: Here's my PID: %dn", getpid());
  101. printf("CHILD: My parent's PID is: %dn", getppid());
  102. printf("CHILD: The value of my copy of childpid is: %dn", childpid);
  103. int *h_a, *d_a;
  104. h_a = (int *)malloc(sizeof(int));
  105. cudaSetDevice(0);
  106. cudaCheckErrors("CHILD cudaSetDevice fail");
  107. cudaMalloc(&d_a, sizeof(int));
  108. cudaCheckErrors("cudaMalloc fail");
  109. *h_a = 1;
  110. cudaMemcpy(d_a, h_a, sizeof(int), cudaMemcpyHostToDevice);
  111. cudaCheckErrors("cudaMemcpy H2D fail");
  112. addkernel<<<1,1>>>(d_a);
  113. cudaDeviceSynchronize();
  114. cudaCheckErrors("kernel fail");
  115. cudaMemcpy(h_a, d_a, sizeof(int), cudaMemcpyDeviceToHost);
  116. cudaCheckErrors("cudaMemcpy D2H fail");
  117. printf("CHILD: result: %dn", *h_a);
  118.  
  119. printf("CHILD: Sleeping for 1 second...n");
  120. sleep(1); /* sleep for 1 second */
  121. cudaDeviceReset();
  122. printf("CHILD: Enter an exit value (0 to 255): ");
  123. scanf(" %d", &retval);
  124. printf("CHILD: Goodbye!n");
  125. exit(retval); /* child exits with user-provided return code */
  126. }
  127. else /* fork() returns new pid to the parent process */
  128. {
  129. printf("PARENT: I am the parent process!n");
  130. printf("PARENT: Here's my PID: %dn", getpid());
  131. printf("PARENT: The value of my copy of childpid is %dn", childpid);
  132. printf("PARENT: I will now wait for my child to exit.n");
  133. int *h_a, *d_a;
  134. h_a = (int *)malloc(sizeof(int));
  135. cudaSetDevice(1);
  136. cudaCheckErrors("PARENT cudaSetDevice fail");
  137. cudaMalloc(&d_a, sizeof(int));
  138. cudaCheckErrors("cudaMalloc fail");
  139. *h_a = 2;
  140. cudaMemcpy(d_a, h_a, sizeof(int), cudaMemcpyHostToDevice);
  141. cudaCheckErrors("cudaMemcpy H2D fail");
  142. addkernel<<<1,1>>>(d_a);
  143. cudaDeviceSynchronize();
  144. cudaCheckErrors("kernel fail");
  145. cudaMemcpy(h_a, d_a, sizeof(int), cudaMemcpyDeviceToHost);
  146. cudaCheckErrors("cudaMemcpy D2H fail");
  147. printf("PARENT: result: %dn", *h_a);
  148. wait(&status); /* wait for child to exit, and store its status */
  149. printf("PARENT: Child's exit code is: %dn", WEXITSTATUS(status));
  150. cudaSetDevice(0);
  151. cudaCheckErrors("PARENT cudaSetDevice 2 fail");
  152. int *h_a2, *d_a2;
  153. cudaMalloc(&d_a2, sizeof(int));
  154. cudaCheckErrors("cudaMalloc fail");
  155. h_a2 = (int *)malloc(sizeof(int));
  156. *h_a2 = 5;
  157. cudaMemcpy(d_a2, h_a2, sizeof(int), cudaMemcpyHostToDevice);
  158. cudaCheckErrors("cudaMemcpy H2D fail");
  159. addkernel<<<1,1>>>(d_a2);
  160. cudaDeviceSynchronize();
  161. cudaCheckErrors("kernel fail");
  162. cudaMemcpy(h_a2, d_a2, sizeof(int), cudaMemcpyDeviceToHost);
  163. cudaCheckErrors("cudaMemcpy D2H fail");
  164. printf("PARENT: result2: %dn", *h_a2);
  165. printf("PARENT: Goodbye!n");
  166. exit(0); /* parent exits */
  167. }
  168. }
  169. else /* fork returns -1 on failure */
  170. {
  171. perror("fork"); /* display error message */
  172. exit(0);
  173. }
  174. }
  175. $ nvcc -arch=sm_20 -o t345 t345.cu
  176. $ ./t345
  177. CHILD: I am the child process!
  178. CHILD: Here's my PID: 23603
  179. CHILD: My parent's PID is: 23602
  180. CHILD: The value of my copy of childpid is: 0
  181. PARENT: I am the parent process!
  182. PARENT: Here's my PID: 23602
  183. PARENT: The value of my copy of childpid is 23603
  184. PARENT: I will now wait for my child to exit.
  185. CHILD: result: 2
  186. CHILD: Sleeping for 1 second...
  187. PARENT: result: 3
  188. CHILD: Enter an exit value (0 to 255): 10
  189. CHILD: Goodbye!
  190. PARENT: Child's exit code is: 10
  191. PARENT: result2: 6
  192. PARENT: Goodbye!
  193. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement