示例#1
0
文件: main.c 项目: hydroo/barrier
static void measureDisseminationBarrier(Context *c, Bool autoPrint) {
    if (autoPrint == True) printf("# %i %s:\n", c->processIndex, __func__);

    int64_t repetitions_;

    void prepare(MPI_Comm comm) {(void) comm;}
    void finalize(MPI_Comm comm) {(void) comm;}
    void f(MPI_Comm comm) {
        int processIndex; MPI_Comm_rank(comm, &processIndex);
        int processCount; MPI_Comm_size(comm, &processCount);

        struct timespec begin, end;

        clock_gettime(CLOCK_REALTIME, &begin);
        const time_t supposedEnd = begin.tv_sec + c->minWallSecondsPerMeasurement;

        for(int64_t repetitions = 0;; repetitions += 1) {

            disseminationBarrier(comm, processIndex, processCount);

            if (repetitions % (3 * 3000) == 0) {
                // incorrect but works since we are constantly syncronizing threads
                // normally some kind of communication between threads needs to happen
                // to consistently exit this loop together
                clock_gettime(CLOCK_REALTIME, &end);
                if (end.tv_sec > supposedEnd) {
                    repetitions_ = repetitions;
                    break;
                }
            }
        }
    }
示例#2
0
int main(int argc, char** argv)
{

    double startTime,endTime;

//    printf("\nEnter the number of threads: ");
//    scanf("%d", &numberOfThreads);

//    printf("Enter the number of barriers: ");
//    scanf("%d", &numberOfBarriers);

    if(numberOfThreads <=0 || numberOfBarriers <= 0) {
      printf("\nERROR: Number of threads/barriers cannot be negative!");
      exit(1);
    }


    pthread_mutex_init(&lock,NULL);

    int k,l;
    
    int barrierArray[numberOfBarriers]; 
    omp_set_num_threads(numberOfThreads);
    int rounds = ceil(log(numberOfThreads)/log(2));
    Thread** threadList = malloc(numberOfThreads*sizeof(Thread*));
 
    for(k = 0; k < numberOfThreads; k++){
        Thread* thread;
        thread = (Thread*) malloc(sizeof(Thread));
        thread->receivingCount = 0;
        thread->sentCount = 0;
        thread->threadID = k;
        *(threadList + k) = thread;
    } 

    for(l = 0;l < numberOfBarriers; l++) 
     	barrierArray[l] =  0;

    //Thread Logic begins here
    #pragma omp parallel shared(numberOfThreads,rounds)
    {
      //Update numberOfThreads to the correct value
	int i = 0,j;
        numberOfThreads = omp_get_num_threads(); 
        int threadId = omp_get_thread_num();
        Thread* thread = *(threadList + threadId);
 
        for(i = 0; i <numberOfBarriers; i++) {
          //Busy Wait
            j = 0;
            while(j < 9999){
                j++;
            }
            printf("Entered thread %d of %d threads at barrier %d\n", thread->threadID, numberOfThreads-1, i);
	  
	   startTime = omp_get_wtime();
           disseminationBarrier(thread,threadList,threadId);
	   printf("Completed thread %d of %d threads at barrier %d\n", thread->threadID, numberOfThreads-1, i);
	   pthread_mutex_lock(&lock);
	   barrierArray[i] = barrierArray[i] + 1;
	   printf("barrierCheck is %d\n", barrierArray[i]);  
	   pthread_mutex_unlock(&lock);
           while(barrierArray[i]!= numberOfThreads);
	   endTime = omp_get_wtime();     
	}
        printf("Time spent in barrier by thread %d is %f\n",thread->threadID, endTime-startTime);
    }
return 0;
}