我是新来的,而且我在编程方面也相对较新。我已经用 C 语言编写了一个程序,我需要使用 pthread 对其进行加速。我曾尝试使用 OpenMP 这样做,但我不知道如何调试它。此外,我需要确定程序是否使用 pthreads 和时间更快,但我不知道如何在我的代码中编写它。这是我的代码
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*/
}
对不起,我的邮件搞砸了,但我是新来的,现在我要开始使用它了