cpuÊÇË«ºËµÄ....
------------------------------
Àí½âÖÐ,2¸öÏ̵߳ÄÔËËãʱ¼äÓ¦¸Ã²î²»¶àÊÇ´®ÐеÄÒ»°ë°¡.ÒòΪÁ½¸öÏ̷ֱ߳𱻵÷µ½2¸öcpuºËÖзֱðÈ¥ÔËÐÐÁË°¡.ÄÚºËÖÐʹÓõÄÊÇNPTLÏ߳̿⡣
½á¹ûÔõô»áÕâÑù?
(³ÌÐòÉè¼ÆÓ¦¸ÃûÓÐÎÊÌâ.)
---------------------------------------------------
#include
#include
#include
#include
const int MATRIX_SIZE=1000; //¾ØÕó´óС
typedef int DataType;
DataType **matrixA,**matrixB,**result;
void InitMatrix(DataType ***matrix) //·ÖÅä¶þλÊý×éµÄ¿Õ¼ä²¢³õʼ»¯
{
*matrix=(DataType**)malloc(sizeof(DataType*)*MATRIX_SIZE);
int i,j;
for(i=0; i
srand((unsigned)time(NULL));
for(i=0; i
}
void FreeMatrix(DataType **matrix)
{
int i;
for(i=0; i
free(matrix);
}
//´®ÐÐ
void Calculate()
{
int i,j,k;
double start, finish;
start=(double)clock();
for(i=0; i
result[i][j]=0;
for(k=0; k
}
finish=(double)clock();
//printf("total time=%.4lf\n", (finish-start)/CLOCKS_PER_SEC);
}
//²¢ÐÐ
const int NumThread=2; //Ï̸߳öÊý
void* threadhandle(void *dum)
{
int i,j,k;
int start=*((int*)dum);
int stop=start+500;
for(i=start; i
result[i][j]=0;
for(k=0; k
}
}
//²¢ÐмÆËã:´´½¨2¸öÏß³Ì
void Calculate_Parallel()
{
int i;
pthread_t pid[NumThread];
double start, finish;
start=(double)clock();
for(i=0; i
finish=(double)clock();
pthread_join(pid[i], NULL);
printf("total time=%.4lf\n", (finish-start)/CLOCKS_PER_SEC);
}
int main()
{
InitMatrix(&matrixA);
InitMatrix(&matrixB);
InitMatrix(&result);
Calculate();
Calculate_Parallel();
FreeMatrix(matrixA);
FreeMatrix(matrixB);
FreeMatrix(result);
}
faniyou ÓÚ 2008-07-29 15:18:42·¢±í:
ʹÓà pthread_join(pid, NULL); ½«µ¼ÖÂÏ߳̽áÊøÇ°ÎÞ·¨·µ»Ø
ËùÒÔ³ÌÐòµÄ×îÖÕ½á¹ûÊÇ˳ÐòÖ´ÐУ¬Ã»ÓÐÓõ½¶àÏ̲߳¢Ðд¦ÀíµÄÓÅÊÆ£¬ËùÒÔʱ¼äÒ»Ñù¡£