The following is a single threaded program (without much error checking) to comp
ID: 3529685 • Letter: T
Question
The following is a single threaded program (without much error checking) to compute ?. You need to convert this program into a multithreaded program that is optimized for a quad-core system. But the catch is that you are not allowed to use locks, mutexes, or any other synchronization primitives. How will you ensure the correctness of your program? (Note: you don't need add comprehensive error checking. Just make sure your solution is free of race conditions.) #include #include #include #include #include uint64_t hit; uint64_t run; int main(int argc, char* argv[]) { /* Seed the random number generator */ srand(time(NULL)); for (run = 0; run < UINT32_MAX; run++) { double x = (double)rand()/(double)RAND_MAX; double y = (double)rand()/(double)RAND_MAX; if (sqrt(x*x + y*y) <= 1) { hit++; } } printf("Pi is %f ", 4*(double)hit/(double)run); }Explanation / Answer
#include #include #include #include #include #include #include #include #include #include static char *options = "df:hi:t:s"; static char *usage = "[-d] [-f outputfile] [-h] [-s] [-t throws] -i iterations -d, turn on debugging messages -f , where arg is a file name for output -h, print this help message and exit -i , number of iterations between printing estimate -t , number of throws per iteration (default=100000) -s, print timing statistics"; #define SQR(a) ((a)*(a)) /* ----| Globals for command line processing |---- */ int dflg = 0, fflg = 0, hflg = 0, iflg = 0, tflg = 0, sflg = 0, errflg = 0; char output[256]; int iter = 0; int throws = 100000; /* ----| Required by getopt() |---- */ extern char *optarg; extern int optind, opterr; long ntime(void) /* ----| Returns a long integer scaled to milliseconds |---- */ { static struct timeval tp; gettimeofday(&tp, (struct timezone *)0); return (tp.tv_sec*1000 + tp.tv_usec / 1000); } void process_command_line(int argc, char *argv[]) { char ch; /* ----| Parse arguments. Flags that are followed by * ----| a colon require an argument, e.g. -f output */ while ((ch = getopt(argc, argv, options)) != EOF) { switch (ch) { case 'd': dflg++; break; case 'f': strcpy(output, optarg); fflg++; break; case 'h': hflg++; break; case 'i': iter = atoi(optarg); if (iter < 1) { fprintf(stderr, "invalid iterations = %d ", iter); errflg++; } iflg++; break; case 't': throws = atoi(optarg); if (throws < 1) { fprintf(stderr, "invalid throws = %d ", throws); errflg++; } tflg++; break; case 's': sflg++; break; default: errflg++; break; } } /* ----| Check for manditory arguments and errors ----| */ if (!(iflg) || hflg || errflg) { fprintf(stderr, "usage : %s %s ", argv[0], usage); exit(1); } } unsigned throw_darts(void) /*---- | Returns the total number of "darts" that hit within *---- | a quarter of a circle of radius 1. */ { unsigned hit = 0; int j; double x, y; for (j = 0 ; jRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.