Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. $ cat t592.cu
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <thread>
  7. #include <mutex>
  8. #include <iostream>
  9. #include <unistd.h>
  10.  
  11. std::mutex mutexCudaExecution;
  12.  
  13. __global__ void testCuda() {}
  14.  
  15. void runCuda()
  16. {
  17. testCuda<<<1, 1>>>();
  18. cudaDeviceSynchronize();
  19. }
  20.  
  21. void wrapperLock()
  22. {
  23. std::lock_guard<std::mutex> lock(mutexCudaExecution);
  24.  
  25. // changing this value to 20000 does NOT trigger "Segmentation fault"
  26. usleep(5000);
  27. runCuda();
  28. }
  29.  
  30. // The thread configuration structure.
  31. typedef struct
  32. {
  33. int device;
  34. pthread_t thread;
  35. cudaError_t status;
  36. }
  37. config_t;
  38.  
  39.  
  40. // The function executed by each thread assigned with CUDA device.
  41. void *thread_func(void *arg)
  42. {
  43. // Unpack the config structure.
  44. config_t *config = (config_t *)arg;
  45.  
  46. int device = config->device;
  47. pthread_t my_thread=config->thread;
  48.  
  49. cudaError_t cuda_status = cudaSuccess;
  50. cuda_status = cudaSetDevice(device);
  51. if (cuda_status != cudaSuccess) {
  52. fprintf(stderr, "Cannot set focus to device %d, status = %d\n",
  53. device, cuda_status);
  54. config->status = cuda_status;
  55. pthread_exit(NULL);
  56. }
  57.  
  58.  
  59. printf("thread %d initialized\n", my_thread);
  60.  
  61. wrapperLock();
  62.  
  63. printf("thread %d deinitialized\n", my_thread);
  64.  
  65. config->status = cudaSuccess;
  66. return NULL;
  67. }
  68.  
  69. int main(int argc, char* argv[])
  70. {
  71. int nthreads = 25;
  72.  
  73. // Create workers configs. Its data will be passed as
  74. // argument to thread_func.
  75. config_t* configs = (config_t*)malloc(sizeof(config_t) * nthreads);
  76.  
  77. // For each CUDA device found create a separate thread
  78. // and execute the thread_func.
  79. for (int i = 0; i < nthreads; i++) {
  80. config_t *config = configs + i;
  81. config->device = 0;
  82. //config->in_host = in + np * i;
  83.  
  84. int status = pthread_create(&config->thread, NULL, thread_func, config);
  85. if (status) {
  86. fprintf(stderr, "Cannot create thread for device %d, status = %d\n",
  87. i, status);
  88. return status;
  89. }
  90. }
  91.  
  92. // Wait for device threads completion.
  93. // Check error status.
  94. int status = 0;
  95. for (int i = 0; i < nthreads; i++) {
  96. pthread_join(configs[i].thread, NULL);
  97. status += configs[i].status;
  98. }
  99. if (status)
  100. return status;
  101.  
  102. free(configs);
  103.  
  104. return 0;
  105. }
  106. $ nvcc -std=c++11 -arch=sm_20 -o t592 t592.cu
  107. $ cuda-memcheck ./t592
  108. ========= CUDA-MEMCHECK
  109. thread 1093793536 initialized
  110. thread 1068615424 initialized
  111. thread 1085400832 initialized
  112. thread 1077008128 initialized
  113. thread 1060222720 initialized
  114. thread 1051830016 initialized
  115. thread 1102186240 initialized
  116. thread 951117568 initialized
  117. thread 934332160 initialized
  118. thread 1018259200 initialized
  119. thread 900761344 initialized
  120. thread 1009866496 initialized
  121. thread 1001473792 initialized
  122. thread 993081088 initialized
  123. thread 984688384 initialized
  124. thread 976295680 initialized
  125. thread 967902976 initialized
  126. thread 959510272 initialized
  127. thread 942724864 initialized
  128. thread 1035044608 initialized
  129. thread 1043437312 initialized
  130. thread 925939456 initialized
  131. thread 917546752 initialized
  132. thread 909154048 initialized
  133. thread 1026651904 initialized
  134. thread 1093793536 deinitialized
  135. thread 1068615424 deinitialized
  136. thread 1085400832 deinitialized
  137. thread 1077008128 deinitialized
  138. thread 1060222720 deinitialized
  139. thread 1051830016 deinitialized
  140. thread 1102186240 deinitialized
  141. thread 951117568 deinitialized
  142. thread 934332160 deinitialized
  143. thread 1018259200 deinitialized
  144. thread 900761344 deinitialized
  145. thread 1009866496 deinitialized
  146. thread 1001473792 deinitialized
  147. thread 993081088 deinitialized
  148. thread 984688384 deinitialized
  149. thread 976295680 deinitialized
  150. thread 967902976 deinitialized
  151. thread 959510272 deinitialized
  152. thread 942724864 deinitialized
  153. thread 1035044608 deinitialized
  154. thread 1043437312 deinitialized
  155. thread 925939456 deinitialized
  156. thread 917546752 deinitialized
  157. thread 909154048 deinitialized
  158. thread 1026651904 deinitialized
  159. ========= ERROR SUMMARY: 0 errors
  160. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement