Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Program Goal: Multithreaded Sieve of Eratosthenes I need help getting this to work multithreaded static uint32_t val = 10240; static BitBlock_t b[4]; static int

C Program

Goal: Multithreaded Sieve of Eratosthenes

I need help getting this to work multithreaded

static uint32_t val = 10240; static BitBlock_t b[4]; static int num_threads = 1; static unsigned short isVerbose;

void setbit (BitBlock_t a[], uint32_t k) { pthread_mutex_lock(&a[k/32].mutex); a[k/32].bits |= 1 << (k%32); pthread_mutex_unlock(&a[k/32].mutex); }

void clearbit (BitBlock_t a[], uint32_t k) { a[k/32].bits &= ~(1 << (k%32)); }

int testbit (BitBlock_t a[], uint32_t k) { // returns true if it was set to 1 int test = -1; pthread_mutex_lock(&a[k/32].mutex); test = ((a[k/32].bits & (1 << (k%32))) != 0); pthread_mutex_unlock(&a[k/32].mutex); return test; //return ((a[k/32].bits & (1 << (k%32))) != 0); }

void * sieveoferatosthenes (void * vid) { long tid = (long) vid; for (uint32_t j = tid; j <= val; j += num_threads) { for (uint32_t p = 2; p *p <= val; p++) {

// pthread_mutex_lock(&b[p/32].mutex); if (testbit(b, p) != 1) { for (uint32_t i = p * p; i <= val; i += p) { setbit(b, i); } } // pthread_mutex_unlock(&b[p/32].mutex); } }

pthread_exit(EXIT_SUCCESS);

}

int main(int argc, char *argv[]) { int opt = -1; pthread_t *threads = NULL; long tid = 0;

for(int i = 0; i < 4; i++) { pthread_mutex_init(&b[i].mutex, NULL); b[i].bits = 0; }

while ((opt = getopt(argc, argv, "t:u:hv")) != -1) { switch (opt) { case 't': num_threads = atoi(optarg); break; case 'u': val = atoi(optarg); break; case 'h': printf("%s: -t # -u # -h -v ", argv[0]); printf("\t-t # for number of threads "); printf("\t-u # for the upper bound of largest number "); printf("\t-h for help (you are already here!) "); printf("\t-v for verbose mode "); exit(0); break; case 'v': isVerbose = 1; break; default: /* '?' */ exit(EXIT_FAILURE); break; }

}

threads = malloc(num_threads * sizeof(pthread_t)); if(isVerbose) { fprintf(stderr, "number of threads: %d", num_threads); fprintf(stderr, "upper bound: %d", val); }

for (tid = 0; tid < num_threads; tid++) { pthread_create(&threads[tid], NULL, sieveoferatosthenes, (void *) tid); }

for (tid = 0; tid < num_threads; tid++) { pthread_join(threads[tid], NULL); }

for (uint32_t p = 2; p <= val; p++) { if (testbit(b, p) != 1) printf("%u ", p); }

free(threads); for(int i = 0; i < 4; i++) { pthread_mutex_destroy(&b[i].mutex); }

return EXIT_SUCCESS; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Differentiate the function. F(t) = (ln t) 2 sin t

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago