/********************************************************/ /*author: Jun Tao */ /* CS 4331 Signal Detection Nov. 30, 2009 */ /* */ /* Compile: upcc -o sigupc sigupc.c */ /* Run: upcrun -n 1 sigupc */ /* */ /********************************************************/ #include #include #include #include #include #define SIZE 25*THREADS // The number of bars in the histogram #define EPSILON 0.001 // The convergence cut-off #define ITERATIONS 50000 // The number of samples in the histogram int foo( int n ) { double r, s, t; do { // Pick a "random" point s on the curve of a function // f(r) such as r^2, a step function, or sin(r). // We assume 0.0 <= f(r) < 1.0 for 0.0 <= r < 1.0. r = drand48(); // (Pick your favorite function or try one of your own.) // s = r*r; // s = (r > 1.0/3.0) ? 1.0 : 0.5; s = ((sin(r * 4.0 * 3.14159) + 1.0) / 2.0); // What are the odds that a second random number t is // less than f(r)? t = drand48(); } while (s < t); // Scale r to the size of the histogram array. r = (r == 1.0) ? 0.99999999 : r; return (int) floor((r * (float)n)); } // Recommended method of computing elapsed time // from GNU documentation of gettimeofday(). int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) // Subtract the `struct timeval' values X and Y, // storing the result in RESULT. // Return 1 if the difference is negative, otherwise 0. { // Perform the carry for the later subtraction by updating y. if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } // Compute the time remaining to wait. // tv_usec is certainly positive. result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; // Return 1 if result is negative. return x->tv_sec < y->tv_sec; } shared [*] int hist[SIZE]; //shared [*] double Signal[SIZE]; //shared int max; //shared double error; //shared int total ; //shared int sum =0; int main () { // double diff; int trial; // struct timeval beginTime, endTime, elapsedTime; int i, s; // int i, j, s; // long seed = 0xFACADE*(MYTHREAD+1); // Initialize drand48() and the signal array. // srand48(seed); // for (i=0; i SIZE-1 ) // { // printf("Error: s=%d\n", s); // break; // } ++hist[s];//race version //bupc_atomicI64_fetchadd_relaxed(&hist[s], (int64_t)1);//race-free version } } upc_barrier; //compute max // upc_forall(i=0; i < SIZE; i++; i){ // max = (hist[i] > max) ? hist[i] : max; // } // upc_barrier; /* error = 0.0; upc_forall(i=0; i error) error = diff; } upc_barrier; if(MYTHREAD == 0){ printf("Trial %d: max error %f at thread%i\n", trial, error, MYTHREAD); } */ ++trial; } while (error > EPSILON); // Stop the clock and print the elapsed time. /* gettimeofday(&endTime,NULL); timeval_subtract (&elapsedTime, &endTime, &beginTime); upc_barrier; // (Be careful to print the leading 0's in tv_usec). printf("this is from thread%i\n", MYTHREAD); printf("Elapsed time = %ld.%06ld \n", elapsedTime.tv_sec, elapsedTime.tv_usec); */ // Do a little ASCII graphics. Print only the odd "rows". /**just print one output **************/ /* if(MYTHREAD == 0){ for (j=0; j=0; i-=2) { printf("|"); // left border for (j=0; j= i) printf("|"); else printf(" "); printf("|\n"); // right border } for (j=0; j