- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请求:每个进程需要计算自己的组到所有点的距离。我的代码如下:
#include stdio.h
#include stdlib.h
#include math.h
#include string.h
#include "mpi.h"
double **malloc_Array2D(int row, int col)
{
int size = sizeof(double);
int point_size = sizeof(double*);
double **arr = (double **) malloc(point_size * row + size * row * col);
if (arr != NULL)
{
memset(arr, 0, point_size * row + size * row * col);
double *head = (double*)(arr + row);
while (row--)
arr[row] = head + row * col;
}
return (double**)arr;
}
void free_Aarray2D(void **arr)
{
if (arr != NULL)
free(arr);
}
double distance(double *pos1, double *pos2, int dim)
{
int i;
double dist = 0.0;
for(i=0;i<dim;i++)
dist += pow((pos2[i]-pos1[i]), 2.0);
return sqrt(dist);
}
int main(int argc,char *argv[])
{
int np, myid;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &np);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
//open file
FILE *fp;
if((fp=fopen("points.dat","r"))==NULL)
{
printf("open file failure! exit!\n");
return -1;
}
//read the number of points
int npoints;
fscanf(fp, "There are %d points\n", &npoints);
if(0==myid)
printf("There are %d points\n", npoints);
int nptsl_max = npoints/np;
if((npoints%np)!=0)
nptsl_max++;
double (*xi)[3];
double **dist;
int *ind = (int *)malloc(sizeof(int)*nptsl_max);
xi = (double (*)[3])malloc(sizeof(double)*nptsl_max*3);
dist= malloc_Array2D(nptsl_max, npoints);
int nptsl = 0; //local number of points
char str[200];
for(int i=0; i<npoints; i++)
{
if(myid == (i%np))
{
fscanf(fp, "%d %lf %lf %lf", ind+nptsl, &xi[nptsl][0], &xi[nptsl][1], &xi[nptsl][2]);
nptsl++;
}
else
fgets(str, 200, fp);
}
fclose(fp);
for(int i=0; i<nptsl; i++)
printf("point %4d on process %d\n", *(ind+i), myid);
dist = (myid == (np-1))?0 :(myid+1);
source = (myid == 0) ?(np-1):(myid-1);
double (*yi)[3];
yi = (double (*)[3])malloc(sizeof(double)*nptsl_max*3);
for(int i=0; i<nptsl; i++)
for(int j=0; j<3; j++)
yi[i][j] = xi[i][j];
for(int loop=0; loop < np)
{
for(int i=0; i<nptsl; i++)
{
for(int j=0; j<npoints; j++)
{
dist[i][j] = distance(xi[i], xi[j], 3);
}
}
}
sprintf(filename, "dist_%d.dat",myid);
fp = fopen(filename, "w");
for(i=0; i<npoints; i++)
{
fprintf(fp, "%4d", ind[i]);
for(j=0; j<npoints; j++)
{
fprintf(fp, " %f", dist[i][j]);
}
fprintf(fp, "\n");
}
fclose(fp);
free(ind);
free(xi);
free_Aarray2D((void **)dist);
}
我不知道如何传达消息。我认为应该使用MPI_Bcast或MPI_Gather进行通信。但我一直无法解决这个问题。有人可以帮助我吗?谢谢!
最佳答案
所以每个进程都从文件中读取一个子集,并且您想计算子集中每个点到所有点的距离?
为什么会有一个带有“循环”变量的循环?
如果您需要进行一次迭代来计算距离,那么让每个进程完全读取整个文件、计算子集的距离并使用 MPI_Gather 将所有距离发送到根节点可能会更快。将写入结果。
如果您计划进行多次迭代,在每次迭代中您还更新点的位置,那么您还需要使用 MPI_Allgather 从其他进程发送/接收点。
关于c - MPI : Use mpi to rewrite the program as a parallel program 中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45250168/
是否有等级编号对组编号的概括?对于我的代码,我想创建 MPI::COMM_WORLD 的分层分解。假设我们使用 16 个线程。我使用 MPI::COMM_WORLD.Split 创建 4 个通信器,每
能够使用多个节点执行 MPI 作业以加快流程 这是我目前使用的命令: mpirun --hostfile myhost -np 2 --map-by slot Job.x//只在第一个节点执行 mpi
我想创建一个新的通信器,它只保留处理中使用的行列,如果我有 24 个可用处理器而我只需要 10 个,那么该组应该只保留这 10 个,否则它将保留所有他们。出于某种原因,当我尝试创建一个通信器时,一切都
我正在开发一些程序,而不是在每个节点上有 4 个内核的 4 节点集群上运行。我有一个非常快的 OpenMP 版本的程序,它只在一个集群上运行,我正在尝试使用 MPI 扩展它。由于我的经验有限,我想知道
这是我尝试在 MPI 中编写经典平滑像素平均算法的代码。我几乎让它工作了,但是光晕交换发生了一些奇怪的事情,因为可以看到边缘的线条。我似乎找不到错误。我是否正确地交换了光环?我应该收集最终数组的哪一部
我有兴趣使用 MPI(消息传递接口(interface))实现一种事件驱动的调度队列。我要解决的基本问题是:我有一个主进程,它将作业插入全局队列,每个可用的从进程检索队列中的下一个作业(如果有的话)。
当我们在集群上使用命令 say mpirun -np 4 a.out 启动 MPI 程序时,然后MPI 运行时系统如何跨 CPU 分配进程? 我的意思是,假设它在集群中找到一个空闲的四核 CPU,它会
使用 mpirun 启动 MPI 作业时或 mpiexec ,我可以理解人们如何开始每个单独的过程。但是,如果没有任何编译器魔法,这些包装器可执行文件如何将安排(MPI 通信器)传达给 MPI 进程?
MPI 中的等级和进程有什么区别? 最佳答案 Here是我从中学习所有 MPI 的资源,您可能会发现它很有用。 关于你的问题:流程 是正在运行的程序的实际实例。 MPI 允许您创建逻辑 团体进程,并且
MPI 障碍的时间复杂度是多少?它们是否可以扩展到大量核心(>> 10k)? 最佳答案 屏障复杂性与实现高度相关。它可以是线性的,可以是对数的,也可以是更好或更差。某些架构为某些集体操作提供专用网络,
我正在尝试 MPI,想知道这段代码是否会导致死锁。 MPI_Comm_rank (comm, &my_rank); if (my_rank == 0) { MPI_Send (sendbuf,
我有一个简单的 MPI 代码,可以打印出进程的等级,使用 Intel 编译器和 MPI 库进行编译和链接。然后我在集群的主节点上以交互方式运行它:mpiexec -n 50 ./a.out该节点只有
我正在尝试使用 mpi run 使用扭矩调度程序来运行我的程序。虽然在我的 pbs 文件中我通过 加载了所有库 export LD_LIBRARY_PATH=/path/to/library 但它给出
我在所有等级上都有一个相同长度的数组(假设为 10)。数组中的某些值包含处理器的等级。例如 ... Proc 1: [1 0 0 0 0 1 0 0 0 1] Proc 2: [0 2 2 0 0 0
我想在集群中的每台机器上独立地轻松执行集体通信。假设我有 4 台机器,每台机器有 8 个内核,我的 MPI 程序将运行 32 个 MPI 任务。对于给定的功能,我想要的是: 在每个主机上,只有一个任务
我希望能够获得某个通信器的唯一 ID,但是当您通过 MPI_Comm_split() 调用获得此通信器时,这似乎是一项不可能完成的任务。我知道什么时候 MPI_Comm_split() 被集体调用,每
非阻塞发送/接收立即在 MPI 中返回,操作在后台完成。我看到这种情况发生的唯一方法是当前进程/线程调用/创建另一个进程/线程并将发送/接收代码的图像加载到其中并返回。然后这个新进程/线程完成这个操作
冗长的背景 我正在为心脏电生理学模拟并行处理一些代码。由于用户可以使用内置脚本语言指定他们自己的模拟,我无法知道如何在通信与计算之间进行权衡。为了解决这个问题,我正在制作一种运行时分析器,一旦看到要运
我打算做一些并行计算,我完全是这方面的初学者。我将使用 MPI 来完成并行工作,采用 Master-Slave 模型。我现在有四台机器,希望其中一台作为主节点。但是,我不知道如何指定运行该程序的其他机
我可以在我的大学使用 PelicanHPC 访问集群网络,其中运行各种 MPI 程序,但在家里我想练习编写/使用其他 MPI 程序。有没有办法在我自己的系统上运行 MPI 程序? (我在 Ubuntu
我是一名优秀的程序员,十分优秀!