Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将MPI与pthreads一起使用-奇怪的问题_C_Pthreads_Mpi - Fatal编程技术网

C 将MPI与pthreads一起使用-奇怪的问题

C 将MPI与pthreads一起使用-奇怪的问题,c,pthreads,mpi,C,Pthreads,Mpi,我有这段代码,我尝试使用pthreads和MPI。 我面临一个奇怪的问题。 pthread函数中的dist.start和dist.end必须相同,但不会发生这种情况。 有人知道问题出在哪里吗 对不起我的英语 #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <mpi.h> void *compute_multiplication(void *arg); #d

我有这段代码,我尝试使用pthreads和MPI。 我面临一个奇怪的问题。 pthread函数中的dist.start和dist.end必须相同,但不会发生这种情况。 有人知道问题出在哪里吗

对不起我的英语

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <mpi.h>

void *compute_multiplication(void *arg);

#define NUMTHRDS 2 
#define VECLEN 100
#define SUCCESS 1 
typedef struct
{
 int start;
 int end;
 int rank;
}DIST;
typedef struct
{
 double *a;
 double *b;
 double sum;
 int veclen;
}DOTDATA;

DOTDATA dotstr;
DIST dist;
pthread_t threads[NUMTHRDS];
pthread_mutex_t mutexsum;

int main(int argc, char *argv[])
{
 double *a;
 double *b;
 int i;
 int *status;
 int err;
 int rank;
 int size;
 int div;

 a = (double *)malloc(NUMTHRDS*VECLEN*sizeof(double));
 b = (double *)malloc(NUMTHRDS*VECLEN*sizeof(double));

 err = MPI_Init(&argc, &argv);
 if(err!=MPI_SUCCESS)
  MPI_Abort(MPI_COMM_WORLD, err);

 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
 MPI_Comm_size(MPI_COMM_WORLD, &size);

 div = NUMTHRDS*VECLEN/size;

 for(i=0;i<VECLEN*NUMTHRDS;i++)
 {
  a[i] = 1.0;
  b[i] = 2*a[i];
 }

 dotstr.veclen = VECLEN;
 dotstr.a = a;
 dotstr.b = b;
 dotstr.sum = 0;

 pthread_mutex_init(&mutexsum,NULL);
 pthread_attr_t attr;
 pthread_attr_init(&attr);

 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); 

 for(i=0;i<NUMTHRDS;i++)
 {
  if(i==0)
  {
   dist.start = rank*div;
  }
  if(i==1)
  {
   dist.start = dist.end;
  }
  dist.end = dist.start + div/2;
  dist.rank = rank;

  printf("rank:%d dists:%d diste1:%d\n",rank,dist.start,dist.end);

  pthread_create(&threads[i],&attr,compute_multiplication,&dist);
 }
 pthread_attr_destroy(&attr);

 for(i=0;i<NUMTHRDS;i++)
 {
  int err;
  pthread_join(threads[i],(void *) &status);
 }
 if(rank==0)
 {
  printf("SUM:%f\n",dotstr.sum);
 }

 free(a);
 free(b);

 pthread_mutex_destroy(&mutexsum);
 MPI_Finalize();
 return 0;
}

void *compute_multiplication(void *arg)
{
 int i;
 DIST* input = (DIST *)arg;
 int start = input->start;
 int end = input->end;
 int sum = 0;
 printf("(%d)%d %d\n",input->rank, start,end);

 //for(i=start;i<end;i++)
 //{
 // sum = sum + dotstr.a[i]*dotstr.b[i];
 //}
 //printf("dists:%d diste1:%d\n",dist.start,dist.end);

 pthread_mutex_lock(&mutexsum);

 dotstr.sum = sum;

 pthread_mutex_unlock(&mutexsum);
 pthread_exit((void*) 0);

}
#包括
#包括
#包括
#包括
void*compute_乘法(void*arg);
#定义numthrds2
#定义VECLEN 100
#定义成功1
类型定义结构
{
int启动;
内端;
整数秩;
}区;
类型定义结构
{
双*a;
双*b;
双和;
int veclen;
}DOTDATA;
dotdatadotstr;
区;
pthread_t threads[numthrd];
pthread_mutex_t mutexsum;
int main(int argc,char*argv[])
{
双*a;
双*b;
int i;
国际*地位;
INTERR;
整数秩;
整数大小;
国际部;
a=(双*)malloc(NUMTHRDS*VECLEN*sizeof(双));
b=(双*)malloc(NUMTHRDS*VECLEN*sizeof(双));
err=MPI_Init(&argc,&argv);
如果(错误!=MPI_成功)
MPI_中止(MPI_通信世界,err);
MPI通信等级(MPI通信世界和等级);
MPI_通信大小(MPI_通信世界和大小);
div=NUMTHRDS*向量/大小;
用于(i=0;irank,start,end);

//为了(i=start;i指向每个线程的
dist
参数是指向在顶级声明的单个全局实例的指针。稍后,当
main
中的循环继续并将不同的值写入
dist
中时,将生成并从该参数读取您正在创建的线程。是否希望在中使用
dist
每个线程的站姿,例如通过分配
numthrd
的数组