Advertisement
Guest User

Untitled

a guest
Feb 14th, 2014
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. $ cat t349.cu
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5.  
  6. #define NUM_THREADS 100
  7.  
  8. #define cudaCheckErrors(msg) \
  9. do { \
  10. cudaError_t __err = cudaGetLastError(); \
  11. if (__err != cudaSuccess) { \
  12. fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
  13. msg, cudaGetErrorString(__err), \
  14. __FILE__, __LINE__); \
  15. fprintf(stderr, "*** FAILED - ABORTING\n"); \
  16. exit(1); \
  17. } \
  18. } while (0)
  19.  
  20. __device__ int dcount = 0;
  21. __global__ void kernel()
  22. {
  23. atomicAdd(&dcount, 1);
  24. }
  25.  
  26. void *thread_func(void *id)
  27. {
  28. int my_id = *(int *)id;
  29. kernel<<<1,1>>>();
  30. cudaDeviceSynchronize();
  31. cudaCheckErrors("kernel fail");
  32. printf("*%d", my_id);
  33. *(int *)id = -1;
  34. return NULL;
  35. }
  36.  
  37. int main(void)
  38. {
  39.  
  40. // Make thread
  41. pthread_t pthread[NUM_THREADS];
  42. int tids[NUM_THREADS];
  43.  
  44. // Create and excute pthread
  45. int tid = 0;
  46. for(int i=0; i<NUM_THREADS; i++) {
  47. tids[i] = tid++;
  48. pthread_create(&pthread[i], NULL, thread_func, (void *)(tids+i));
  49. }
  50.  
  51. // Join pthread
  52. for(int i=0; i<NUM_THREADS; i++) {
  53. pthread_join(pthread[i], NULL);
  54. }
  55.  
  56. int hcount = 0;
  57. cudaMemcpyFromSymbol(&hcount, dcount, sizeof(int));
  58. for (int i =0; i<NUM_THREADS; i++)
  59. if (tids[i] != -1) {printf("thread fail at id %d\n", i); return 1;}
  60. printf("\nresult = %d\n", hcount);
  61.  
  62. return 0;
  63. }
  64. $ nvcc -arch=sm_20 -o t349 t349.cu
  65. $ ./t349
  66. *7*2*20*8*5*18*11*1*16*10*22*24*0*6*25*15*23*3*27*19*29*14*30*12*17*9*31*13*4*32*21*33*26*35*28*37*50*54*62*66*71*77*38*45*51*81*67*97*75*68*99*94*52*86*79*53*39*34*55*47*56*58*60*61*40*59*41*65*63*73*64*72*69*70*42*74*36*76*78*43*44*80*46*82*83*84*85*87*88*90*89*93*91*92*95*49*98*57*48*96
  67. result = 100
  68. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement