Guest User

Untitled

a guest
Mar 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. // ----------------------- do multi-threaded versions here ------------------
  2.  
  3.  
  4. /* GLOBALS */
  5. kvp* gsrc;
  6. kvp* gdst;
  7. int gdim;
  8.  
  9. /* Thread stuff */
  10. pthread_barrier_t b1;
  11. pthread_barrier_t b2;
  12.  
  13. /* Other stuff */
  14. int nbits = 8;
  15. int nbuckets = 1 << nbits; // 256
  16. int nsize;
  17. int numThreads = 4;
  18.  
  19. /* idk */
  20. int tid;
  21.  
  22. int mask = 0xFF;
  23.  
  24. unsigned long long gbuckets[256][tid];
  25. // Should this be 2D?
  26. unsigned long gsum[256];
  27.  
  28. /* Peer thread routine */
  29. void thread(void *id_ptr) {
  30.  
  31. int thread_id = *(int)id_ptr;
  32.  
  33. int i, k, bucketIndex, nthread, shift;
  34.  
  35. for (nthread = 0; nthread < numThreads; nthread++) {
  36. pthread_barrier_wait(&b1);
  37. //the main is clearing the global stuff here <-- WTF mate
  38. pthread_barrier_wait(&b1);
  39.  
  40.  
  41. // `fill buckets, this is the same as before without index increment'
  42.  
  43. // ^^ Doesn't make sense to me:
  44.  
  45. // This is how we generated the bucket count in naive_singlethread (L89-93):
  46. /*
  47. for(int i = 0; i < dim; ++i) {
  48. int index = gen_shift(src[i].key,iter*log_radix,
  49. (bucket_size(log_radix)-1))+1;
  50. buckets[index][iter]++;
  51. }
  52. */
  53.  
  54. // But here `iter' is the array of thread ids. I think the peer thread should
  55. // only generate the bucket count for the elements in that row, right?
  56.  
  57. for (i = 0; i < dim; ++i) {
  58. k = gsrc[i].key;
  59. // nbits == log_radix => 8
  60. bucketIndex = ((k >> nthread*nbits) & mask); /* ((k >> 8n) & 0xFF)) */
  61. gbuckets[i][bucketIndex]++;
  62. }
  63.  
  64.  
  65. // new phase : Need to create an offset using buckets and sum
  66. // you basically add the global buckets to the global sum -1 */
  67.  
  68. for (bucketIndex = 0; bucketIndex < nbuckets; ++bucketIndex) {
  69. gbuckets[bucketIndex][nthread] += sum[bucketIndex-1];
  70. }
  71.  
  72. // you will have to traverse each segment backwards
  73. // in order to place them in the right order.
  74. // But this basically works the same as single thread. But you decrement
  75. // instead of increment, and you decrement before you set it to out_index
  76. for (i = dim; i > 0; --i) {
  77. // I'm lost bro.
  78. }
  79. }
  80. return NULL;
  81. }
  82.  
  83. /*
  84. * Shitty Multithread - I'm dying inside.
  85. * Three Phases:
  86. * 1. Generate bucket count for each part - Parallel
  87. * 2. Generate Local Prefix Sum for each part - Parallel .
  88. * 3. Generate Global Sum - Sequential
  89. * 4. Move data.
  90. */
  91. char shitty_multithread_descr[] = "shitty_multithread: last thing you'll see before I sefault.";
  92.  
  93. void shitty_multithread(int dim, kvp *src, kvp *dst)
  94. {
  95. pthread_t tids[numThreads];
  96. int short_ids[numThreads];
  97.  
  98. pthread_barrier_init(&b1, NULL, numThreads);
  99. /* pthread_barrier_init(&b2, NULL, numThreads); */
  100. gdim = dim;
  101. gdst = dst;
  102. gsrc = src;
  103.  
  104. nsize = dim/numThreads;
  105.  
  106. for (i = 0; i < numThreads; i++) {
  107. tid = i;
  108. pthread_create(&tids[i], NULL, thread, &tids[i]);
  109. }
  110.  
  111. pthread_barrier_wait(&b1);
  112. memset(*gbuckets, 0, sizeof(unsigned long long)*nbuckets*(numThreads+1));
  113. memset(*gsum, 0, sizeof(unsigned long)*nbuckets);
  114. int i , j;
  115.  
  116. // Criticial phase
  117. for (i = 1; i < numThreads; i++) {
  118. for (j = 0 ; j < 256; j++) {
  119. gbuckets[j][i] += gbuckets[j][i-1];
  120. }
  121. }
  122.  
  123. for (i = 0; i < numThreads; i++) {
  124. pthread_join(tid[i], NULL);
  125. }
  126.  
  127. pthread_destroy(&b1);
  128. }
Add Comment
Please, Sign In to add comment