Im对使用不同线程号运行Monaco算法的应用程序进行编码,它们分别为2、4、6和8,以计算PI的值。目的也是在使用更多线程时看到速度提高。问题是,我的代码没有划分工作,而是简单地工作了2次!
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
#include <string.h>
#define SEED 35791246
void *monaco(int arcg, char* argv) {
clock_t t1, t2;
float pi;
t1 = clock();
t2 = clock();
float diff = ((float)(t2 - t1) / 1000000.0F );
printf("\n");
printf("Thread number = %d\n", (int)(pthread_self()));
printf("Total Time: %f segundos\n", diff);
printf("\n");
printf("\n");
return 0;
}
void run_test(int num_thread) {
pthread_t pth_arr[num_thread];
int i = 0;
for (i = 0; i < num_thread; i++) {
pthread_create(&pth_arr[i], NULL, monaco, NULL);
}
for (i = 0; i < num_thread; i++) {
pthread_join(pth_arr[i], NULL);
}
}
int main(count) {
int num_thread = 9;
int pontos=20000;
double x,y;
count=0;
double z;
float pi;
int j = 0;
int i = 0;
srand(SEED);
count = 0;
for (i = 1; i < num_thread; i++) {
if (i==1)
{
continue;
}
if (i==3)
{
continue;
}
if (i==5)
{
continue;
}
if (i==7)
{
continue;
}
for (j = 0; j<pontos; j++) {
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
z = x*x+y*y;
if (z<=1) count++;
}
pi=(double)count/pontos * 4;
printf ("Points used are: %d , and the Pi estimate is: %g \n", pontos,pi);
printf ("Using %d threads.\n", i);
run_test(i);
}
return 0;
}
我得到的输出是:
Points used are: 20000 , and the Pi estimate is: 3.1288
Using 2 threads.
Thread number = -1332082944
Total Time: 0.000002 segundos
Thread number = -1340475648
Total Time: 0.000002 segundos
Thread number = -1340475648
Total Time: 0.000002 segundos
Points used are: 20000, and the Pi estimate is: 6.2932
Using 4 threads.
Thread number = -1332082944
Total time: 0.000002 segundos
Thread number = -1348868352
Total time: 0.000001 segundos
Thread number = -1357261056
Total time: 0.000001 segundos
Thread number = -1340475648
Total time: 0.000002 segundos
Points used are: 20000 , and the Pi estimate is: 9.4232
如何使它分工而不是分两次进行?
[在学习的早期阶段(即使您是专家),您需要假定编译器比您更聪明。当按原样编译您的代码时,出现以下错误:
SO.c: In function ‘run_test’:
SO.c:32:39: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
32 | pthread_create(&pth_arr[i], NULL, monaco, NULL);
| ^~~~~~
| |
| void * (*)(int, char *)
In file included from SO.c:3:
/usr/include/pthread.h:236:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int, char *)’
236 | void *(*__start_routine) (void *),
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
SO.c: In function ‘main’:
SO.c:41:5: warning: type of ‘count’ defaults to ‘int’ [-Wimplicit-int]
41 | int main(count) {
| ^~~~
在第一组错误中,编译器正在报告有关不兼容的指针参数的信息。从pthreads手册页:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
pthread_create()
将void* (*func) (void*)
函数指针作为参数,但是您传递的monaco()
函数的类型为void* (*monaco) (int, char*)
。
如果要向start_routine()
传递多个参数,则需要考虑一些替代方法,例如,将它们包装在结构中并将其地址作为参数传递。
关于第二组错误,我很惊讶地看到您的代码首先被编译。我不知道这种声明的含义,其他人需要回答:(
即使更正了所有语法错误,我也不知道您的代码将如何划分线程之间的工作。您正在main()
中进行大部分计算。也许您在代码的初始版本中,但是我不想通过提供完整的代码来破坏乐趣:)
对于错误部分,对于每次迭代,该值都会加倍,因为在迭代开始时您没有将count
初始化为0
。使用此行,在count = 0;
循环的开始处的for
,输出是预期的,但切记在当前状态下绝对不能接受您的代码。
提示:i & 1
是确定奇数的简便方法。