我在這裏是新手,而且在編程方面我還是比較新的。我用C編寫了一個程序,我需要使用pthread來加速它。我嘗試過使用OpenMP,但我不知道如何調試它。此外,我需要找出使用pthreads和時間的程序是否更快,但我不知道如何在代碼中編寫此代碼。這裏是我的代碼使用pthreads加速C程序
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*/
}
對不起,我搞砸了郵件,但我是新她現在我就用它
添加線程_may_不會加速您的程序可能是一個更好的術語 – 2012-03-29 11:56:45
@Pavan Manjunath - 是。我更新了我的帖子。 – mah 2012-03-29 12:03:17
謝謝你的回答,問題是我必須在這個程序中使用pthreads,我會試着更好地理解它,然後我會回覆更多的問題 – Vasouli 2012-03-29 17:32:52