7 Replies - 191 Views - Last Post: 02 March 2013 - 12:27 PM Rate Topic: -----

#1 ckayorke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-March 13

OpenMP to POSIX Threads

Posted 02 March 2013 - 08:21 AM

I am trying to execute the code below using POSIX Threads instead of OpenMp. Any ideas. I am newbee. I have tried several times and it is not working.

   #include <stdio.h>
   #include <math.h>
   #include <omp.h>

  main(int argc, char *argv[])  {
      int i, j, limit;
      int start, end;          /* range of numbers to search */
      int number_of_primes = 0;  /* number of primes found */
      int number_of_41primes = 0;/* number of 4n+1 primes found */
      int number_of_43primes = 0;/* number of 4n-1 primes found */
      int prime;               /* is the number prime? */
      int print_primes = 0;      /* should each prime be printed? */

      start = atoi(argv[1]);
      end = atoi(argv[2]);
      if (!(start % 2))
          start++;

      if (argc == 4 && atoi(argv[3]) != 0)
           print_primes = 1;
       printf("Range to check for Primes: %d - %d ",start, end);

       #pragma omp parallel for private (limit, j, prime)
       for(i = start; i <= end; i += 2) {

            limit = (int) sqrt((float)i) + 1;
            prime = 1; /* assume number is prime */
            j = 3;
            while (prime && (j <= limit)) {
              if (i%j == 0) prime = 0;
              j += 2;
            }

            if (prime) {
            if (print_primes) printf("%5d is prime",i);
            #pragma critical
            {
                number_of_primes++;
                if (i%4 == 1) number_of_41primes++;
                if (i%4 == 3) number_of_43primes++;
            }
         } 
       }

  printf("\n Program Done.\n  %d primes found \n",number_of_primes);
  printf("\nNumber of 4n+1 primes found: %d \n",number_of_41primes);
  printf("\nNumber of 4n-1 primes found: %d \n",number_of_43primes);
}

This post has been edited by jimblumberg: 02 March 2013 - 08:33 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: OpenMP to POSIX Threads

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 08:34 AM

I don't see anything trying to use threads, perhaps you posted the wrong code?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 ckayorke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-March 13

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 08:45 AM

line 23 to 43 must use POSIX threads instead of open Mp #pragma
Was This Post Helpful? -1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 08:53 AM

In other words, "I found some code on the Internet, but my assignment is for pthreads, not OpenMP. Please rewrite this found code so I can hand in my assignment without having to do anything more than a Google search and posting across forums".

Have I got that right?
Was This Post Helpful? 0
  • +
  • -

#5 ckayorke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-March 13

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 11:28 AM

No you actually got me wrong. Below is what I have. The variable number_of_primes gives me wrong output

#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>

pthread_mutex_t mu;
int i, j, limit;   
int number_of_primes = 0;  /* number of primes found */
int number_of_41primes = 0;/* number of 4n+1 primes found */
int number_of_43primes = 0;/* number of 4n-1 primes found */
int prime;               /* is the number prime? */
int print_primes = 0;      /* should each prime be printed? */

void *p(void * arg) {
      assert(pthread_mutex_lock(&mu) == 0);
      limit = (int) sqrt((float)i) + 1;
      prime = 1; /* assume number is prime */
      j = 3;
      while (prime && (j <= limit)) {
         if (i%j == 0) prime = 0;
         j += 2;
      }
 
      if (prime) {
	      if (print_primes)
		  printf("%5d is prime",i);
	      number_of_primes++;
	      if (i%4 == 1)
		   number_of_41primes++;
	      if (i%4 == 3)
		  number_of_43primes++;
              sleep(1);
      }
       assert(pthread_mutex_unlock(&mu) == 0);
       pthread_exit((void *)99);
	
}

int main(int argc, char *argv[]) {
	int start, end;          /* range of numbers to search */
        start = atoi(argv[1]);
        end = atoi(argv[2]);
        const int NUM_THREADS = end;
        if (!(start % 2))
            start++;

        if (argc == 4 && atoi(argv[3]) != 0)
            print_primes = 1;
        printf("Range to check for Primes: %d - %d ",start, end);

        pthread_t x[NUM_THREADS];       
       
        assert(pthread_mutex_init(&mu /* OUT */ , NULL) == 0);
        for(i = start; i< NUM_THREADS; i++){
            void *r;
            assert(pthread_create(&x[i], NULL, p, (void *)i) == 0);
            assert(pthread_join(x[i], &r) == 0);        
        }
        assert(pthread_mutex_destroy(&mu) == 0);
        printf("\n Program Done.\n  %d primes found \n",number_of_primes);
        printf("\nNumber of 4n+1 primes found: %d \n",number_of_41primes);
        printf("\nNumber of 4n-1 primes found: %d \n",number_of_43primes);
	return 0;
}


This is my threaded code. Everything works fine except the variable number_of_primes

#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>

pthread_mutex_t mu;
int i, j, limit;   
int number_of_primes = 0;  /* number of primes found */
int number_of_41primes = 0;/* number of 4n+1 primes found */
int number_of_43primes = 0;/* number of 4n-1 primes found */
int prime;               /* is the number prime? */
int print_primes = 0;      /* should each prime be printed? */

void *p(void * arg) {
      assert(pthread_mutex_lock(&mu) == 0);
      limit = (int) sqrt((float)i) + 1;
      prime = 1; /* assume number is prime */
      j = 3;
      while (prime && (j <= limit)) {
         if (i%j == 0) prime = 0;
         j += 2;
      }
 
      if (prime) {
	      if (print_primes)
		  printf("%5d is prime",i);
	      number_of_primes++;
	      if (i%4 == 1)
		   number_of_41primes++;
	      if (i%4 == 3)
		  number_of_43primes++;
              sleep(1);
      }
       assert(pthread_mutex_unlock(&mu) == 0);
       pthread_exit((void *)99);
	
}

int main(int argc, char *argv[]) {
	int start, end;          /* range of numbers to search */
        start = atoi(argv[1]);
        end = atoi(argv[2]);
        const int NUM_THREADS = end;
        if (!(start % 2))
            start++;

        if (argc == 4 && atoi(argv[3]) != 0)
            print_primes = 1;
        printf("Range to check for Primes: %d - %d ",start, end);

        pthread_t x[NUM_THREADS];       
       
        assert(pthread_mutex_init(&mu /* OUT */ , NULL) == 0);
        for(i = start; i< NUM_THREADS; i++){
            void *r;
            assert(pthread_create(&x[i], NULL, p, (void *)i) == 0);
            assert(pthread_join(x[i], &r) == 0);        
        }
        assert(pthread_mutex_destroy(&mu) == 0);
        printf("\n Program Done.\n  %d primes found \n",number_of_primes);
        printf("\nNumber of 4n+1 primes found: %d \n",number_of_41primes);
        printf("\nNumber of 4n-1 primes found: %d \n",number_of_43primes);
	return 0;
}

This post has been edited by jimblumberg: 02 March 2013 - 11:30 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Was This Post Helpful? 1
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,775
  • Joined: 25-December 09

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 11:30 AM

Please use code tags when posting code.

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#7 ckayorke  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-March 13

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 11:40 AM

Sorry. Below is the code again

#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>

pthread_mutex_t mu;
int i, j, limit;   
int number_of_primes = 0;  /* number of primes found */
int number_of_41primes = 0;/* number of 4n+1 primes found */
int number_of_43primes = 0;/* number of 4n-1 primes found */
int prime;               /* is the number prime? */
int print_primes = 0;      /* should each prime be printed? */

void *p(void * arg) {
      
      limit = (int) sqrt((float)i) + 1;
      prime = 1; /* assume number is prime */
      j = 3;
      while (prime && (j <= limit)) {
         if (i%j == 0) prime = 0;
         j += 2;
      }
       assert(pthread_mutex_lock(&mu) == 0);
      if (prime) {
	      if (print_primes)
		  printf("%5d is prime",i);
	      number_of_primes++;
	      if (i%4 == 1)
		   number_of_41primes++;
	      if (i%4 == 3)
		  number_of_43primes++;
              sleep(1);
      }
       assert(pthread_mutex_unlock(&mu) == 0);
       pthread_exit((void *)99);
	
}

int main(int argc, char *argv[]) {
	int start, end;          /* range of numbers to search */
        start = atoi(argv[1]);
        end = atoi(argv[2]);
        const int NUM_THREADS = end;
        if (!(start % 2))
            start++;

        if (argc == 4 && atoi(argv[3]) != 0)
            print_primes = 1;
        printf("Range to check for Primes: %d - %d ",start, end);

        pthread_t x[NUM_THREADS];       
       
        assert(pthread_mutex_init(&mu /* OUT */ , NULL) == 0);
        for(i = start; i< NUM_THREADS; i++){
            void *r;
            assert(pthread_create(&x[i], NULL, p, (void *)i) == 0);
            assert(pthread_join(x[i], &r) == 0);        
        }
        assert(pthread_mutex_destroy(&mu) == 0);
        printf("\n Program Done.\n  %d primes found \n",number_of_primes);
        printf("\nNumber of 4n+1 primes found: %d \n",number_of_41primes);
        printf("\nNumber of 4n-1 primes found: %d \n",number_of_43primes);
	return 0;
}
      



Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,238
  • Joined: 23-August 08

Re: OpenMP to POSIX Threads

Posted 02 March 2013 - 12:27 PM

Excellent, I'm glad to see you did put in an effort! Too many think what I stated in my first post is a winning strategy. It is definitely not.

So, that said (and it's been a while since I've done pthreads), I note the following.

First: you mention an inssue with number_of_primes, but not what the issue is.
Second: locking for the entirety of the thread function is counter-productive. You want your locking to be as granular as possible.
Third: too many globals. I think some of those variables should/need to be local to the thread function itself.
Fourth: you're passing an argument to the thread function but not using it.
Fifth: I think the idea here is to split the range of primes up equally amongst the number of threads. You're not doing that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1