0

I'm new here, and also I'm relatively new in programming in general. I' ve writen a program in C and I need to accelerate it using pthreads. I've tried to do so using OpenMP, but I don't know how to debug it. Also I need to find out if the programm is faster using pthreads and the times, but I don't know how to write this in my code. Here is my code

enter code here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define NTHREADS 2
#define FYLLO(komvos) ((komvos) * 2 + 1)

long factorial(long);
void heap_function (int [], int, int );
void make_heap(long [], int );
void pop_heap(long [], int );

struct thread_data
{
long int    n;
long int k;
long *b;
};

main()
{

  long int n,k,c,fact=1;
  long *a,*b,*d,p[k];
  int i,j,rc;
  int q[]={2,3,4,5,6,7,8,9,12,13,14,15,16};
  pthread_t thread[NTHREADS];
  struct thread_data threada;
  for(i=0;i<NTHREADS;i++)
  {
    threada.n=n;
    threada.k=k;
    threada.b=b;
    pthread_create (&thread[i], NULL, (void *)&threada);
  }
  for (i=0; i<NTHREADS; i++) 
    rc = pthread_join (thread[i], NULL);

  for(i=0;i<13;i++)
  {
    k=pow(2,q[i])-1;
    if(a=(long*)malloc(i*sizeof(long))==NULL);
    {
       printf("Den yparxei diathesimi mnimi gia desmeusi\n");
       exit(1);
    }
    a[i]=k;
    for(a[0];a[13];a[i]++)
    {
        n=(pow(2,q[i]))*k;
        if(d=(long*)malloc((i*i)*sizeof(long))==NULL);
        {
            printf("Den yparxei diathesimi mnimi gia desmeusi\n");
            exit(1);
        }
        d[i]=n;
    }  
  c=(factorial(n))/((factorial(k))*(factorial(n-k)));     
  } 
  if(b=(long*)malloc(((i*i)+i)*sizeof(long))==NULL)
    {
        printf("Den yparxei diathesimi mnimi gia desmeusi\n");
        exit(1);
    }
  for(i=0;i<13;i++)
  {
    b[i]=a[i];
  }
  for(i=13;i<182;i++)                     /* Gia i=13 exoume i^2=169 kai i^2+i=182*/
  {
    b[i]=d[i];
  }    
  long heap[sizeof(b)];
  make_heap( heap, sizeof(b) );
  printf("To heap einai:\n");        
    for ( i = sizeof(b); i >=0; i-- ) 
        {
            printf( "%d ", heap[0] );
            pop_heap( heap, i );
        }
    for(i=(n-k);i<=n;i++)
        for(j=0;j<k;j++)
        {
            p[j]=heap[i];
            printf("Ta %d mikrotera stoixeia eina ta %ld\n",k,p[j]);
        } 
  free((void*)b);             
  getch();
}



long factorial(long n)
{
   int a;
   long result=1;

   for( a=1;a<=n;a++ )
     result=result*a;

   return(result);
}



void heap_function( int a[], int i, int n )
{

  while ( FYLLO( i ) < n )                  /* Vazoume sto heap ta stoixeia san           ypodentra */
{

  int fyllo = FYLLO( i );


  if ( fyllo + 1 < n && a[fyllo] < a[fyllo + 1] )     /* Dialegoume to maegalytero apo ta dyo paidia komvous */
  ++fyllo;


  if ( a[i] < a[fyllo] )                      /* Metaferoume to megalytero komvo sti riza */
  {
    int k = a[i];
    a[i] = a[fyllo];
    a[fyllo] = k;
  }


  ++i;                                    /* Synexizoume ston epomeno komvo */
  }
}


void make_heap( long a[], int n )        /*Dhmioyrgoume ti sinartisi make_heap gia na  mporesoume na valoume ta
                                        stoixeia pou dwsame mesa sto heap kai na ta ta3inomisoume*/
{
  int i = n / 2;

  while ( i-- > 0 )
  heap_function( a, i, n );
}


void pop_heap( long heap[], int n )      /*Dhmiourgoume ti sinartisi pop_heap gia na  mporesoume na e3agoume 
                                        ta stoixeia apo to heap apo to megalytero sto mikrotero*/
{
  long k = heap[0];
  heap[0] = heap[n];
  heap[n] = k;
  heap_function( heap, 0, n );          /*Afou emfanistei to prwto stoixeio kaloume ti  sinartisi heap_function 
                                        gia na ta3inomisei ta stoixeia pou menoun sto heap*/
}

Sorry for my messed up mail, but I'm new her now I'm getting to use it

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Vasouli
  • 47
  • 1
  • 8

1 Answers1

5

Adding threads may not accelerate your program, it lets you do organize your work into execution units which can appear to run in parallel (and on multi-core systems, generally can run in parallel). If you're not on a multi-core system you can still gain an advantage if one or more of your threads must block waiting for slow input because other thread(s) can continue to run; this may or may not give you a faster runtime, depending on your actual program.

Debugging threads is generally more difficult than debugging a single thread, and how to do it comes down to the tools you have available. If your debugger is not able to make the job easier for you, I would recommend you first make your program run serially -- still break it up using a threaded model, but let the code for each run in the primary thread and let it run till completion, if your model permits this. Many threaded applications cannot be written like that because threads depend on each other during runtime, but it just depends on what you're doing exactly.

Now to your specific situation -- you're diving into the deep end when you don't know how to swim yet. I would suggest you first learn to use threads without the complexity of why you need them, otherwise you're making the problem more complicated than it needs to be. http://cs.gmu.edu/~white/CS571/Examples/Pthread/create.c has a simple example to get started with. Take particular notice to the parameters of the pthread_create() call and compare to what you've done; your code is missing the 3rd parameter -- the function to run as a thread. You appear to have no such function at all, and instead you seem to believe that the code following the call to pthread_create() is what runs in parallel. This is how fork() works, but that's very different.

That should be enough to get you started. http://cs.gmu.edu/~white/CS571/Examples/pthread_examples.html has additional examples, and a google of "pthread tutorial" would probably be helpful.

mah
  • 39,056
  • 9
  • 76
  • 93
  • 2
    Adding threads _may_ not accelerate your program might be a better term – Pavan Manjunath Mar 29 '12 at 11:56
  • Thank you for your answer, the thing is that I have to use pthreads in this programm, I'll try to understand it better and I'll be back with further questions – Vasouli Mar 29 '12 at 17:32
  • @Vasouli -- of course, but you need to learn to crawl before you can learn to walk. If you try to go straight for walking (i.e., squeezing it into this program at any cost), you'll end up disappointed with your outcome because you didn't know what you were doing. If you work on a tutorial / simple project first, you'll learn a lot and avoid making some mistakes. You'll also gain a better understanding that you have to organize your code better to make it suitable for threading in the first place -- something your post suggests you may need.... nothing complicated, just a little organization. – mah Mar 29 '12 at 20:07
  • @mah -- That's the problem. I've been asked to first write the serial code and then my professor asked to write the threading code. So the code lacks organization. Now I'm looking these tutorials hoping to understand and write the code properly – Vasouli Mar 30 '12 at 08:50