| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <math.h> |
| 5 | #include <pthread.h> |
| 6 | |
| 7 | #include "bio++.H" |
| 8 | #include "meryl.H" |
| 9 | #include "libmeryl.H" |
| 10 | |
| 11 | void |
| 12 | runSegment(merylArgs *args, uint64 segment); |
| 13 | |
| 14 | pthread_mutex_t segmentMutex; |
| 15 | uint64 segmentNext; |
| 16 | uint64 segmentMax; |
| 17 | uint32 *segmentDone; |
| 18 | |
| 19 | |
| 20 | void* |
| 21 | buildThread(void *U) { |
| 22 | uint64 segment = uint32ZERO; |
| 23 | merylArgs *args = (merylArgs *)U; |
| 24 | |
| 25 | while (segment < segmentMax) { |
| 26 | pthread_mutex_lock(&segmentMutex); |
| 27 | segment = segmentNext++; |
| 28 | pthread_mutex_unlock(&segmentMutex); |
| 29 | |
| 30 | if (segment < segmentMax) { |
| 31 | runSegment(args, segment); |
| 32 | segmentDone[segment]++; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if (args->beVerbose) |
| 37 | fprintf(stderr, "Thread exits.\n"); |
| 38 | |
| 39 | return(0L); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void |
| 44 | runThreaded(merylArgs *args) { |
| 45 | |
| 46 | // Clear stuff |
| 47 | // |
| 48 | segmentNext = uint64ZERO; |
| 49 | segmentMax = args->segmentLimit; |
| 50 | segmentDone = new uint32 [segmentMax]; |
| 51 | for (uint64 s=0; s<segmentMax; s++) |
| 52 | segmentDone[s] = uint32ZERO; |
| 53 | |
| 54 | // Initialize threads |
| 55 | // |
| 56 | pthread_attr_t threadAttr; |
| 57 | pthread_t threadID; |
| 58 | |
| 59 | pthread_mutex_init(&segmentMutex, NULL); |
| 60 | |
| 61 | pthread_attr_init(&threadAttr); |
| 62 | pthread_attr_setscope(&threadAttr, PTHREAD_SCOPE_SYSTEM); |
| 63 | pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); |
| 64 | pthread_attr_setschedpolicy(&threadAttr, SCHED_OTHER); |
| 65 | |
| 66 | // Start the threads |
| 67 | // |
| 68 | for (uint64 i=0; i<args->numThreads; i++) |
| 69 | pthread_create(&threadID, &threadAttr, buildThread, (void *)args); |
| 70 | |
| 71 | // Wait for the threads to complete |
| 72 | // |
| 73 | struct timespec shortNap; |
| 74 | shortNap.tv_sec = 1; |
| 75 | shortNap.tv_nsec = 0; |
| 76 | |
| 77 | uint64 s=0; |
| 78 | while (s < segmentMax) { |
| 79 | if (segmentDone[s] == 0) |
| 80 | nanosleep(&shortNap, 0L); |
| 81 | else |
| 82 | s++; |
| 83 | } |
| 84 | |
| 85 | if (args->beVerbose) |
| 86 | fprintf(stderr, "Threads all done, cleaning up.\n"); |
| 87 | |
| 88 | // Cleanup |
| 89 | // |
| 90 | pthread_attr_destroy(&threadAttr); |
| 91 | pthread_mutex_destroy(&segmentMutex); |
| 92 | |
| 93 | delete [] segmentDone; |
| 94 | } |