- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望运行一次效率测试以使用 MPI 找到全局最大值,一次使用环形拓扑并再次使用 MPI_REDUCE 函数。我已经在我的代码中执行了 MPI_REDUCE 并且它工作正常但我想通过环传递产生相同的结果。
我的想法是为每个处理器的局部最大值创建一个数组,然后将这些最大值传递到环中,最后输出全局最大值。
不幸的是,我意识到我无法定义一个单一的数组来保存进程的最大值,而是我最终制作了从 4 个进程生成的 4 个不同的数组。然后我开始从 rank = 0 处理器开始传递 array[0] 值,而不是只传递一个 max ,我必须传递 4 个不同的数组值,因为我生成了 4 个不同的数组。更糟糕的是,即使经过所有这些努力,我也没有得到全局最大值,因为我只从一行 MPI_REDUCE 代码中得到了。必须有一种方法可以从环形拓扑中获得全局最大值,而我只是让事情变得复杂。
主要部分代码如下:
int main(int argc, char **argv)
{
int rank, size;
MPI_Init (&argc, &argv); // initializes MPI
MPI_Comm_rank (MPI_COMM_WORLD, &rank); // get current MPI-process ID. O, 1, ...
MPI_Comm_size (MPI_COMM_WORLD, &size); // get the total number of processes
/* define how many integrals */
const int n = 10;
double b[n] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0,5.0};
double a[n] = {-5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0, -5.0,-5.0};
double result, mean;
int m;
const unsigned int N = 5;
double max = 0;
double max_store[4];
cout.precision(6);
cout.setf(ios::fixed | ios::showpoint);
srand(time(NULL) * rank); // each MPI process gets a unique seed
m = 4; // initial number of intervals
// convert command-line input to N = number of points
//N = atoi( argv[1] );
for (unsigned int i=0; i <=N; i++)
{
result = int_mcnd(f, a, b, n, m);
mean = result/(pow(10,10));
m = m*4;
if( mean > max)
{
max = mean;
}
if ( rank < 4 && rank >= 0 )
{
max_store[rank] = max;
}
}
//print the array containing max from each processor
for( int k = 0; k < 4; k++ )
{
printf( "%1.5e\n", max_store[k]);
}
printf("Process ID %i, local_max = %f\n",rank, max);
// All processes get the global max, stored in place of the local max
MPI_Allreduce( MPI_IN_PLACE, &max, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
printf("Process ID %d, global_max = %f\n",rank, max);
double send_junk = max_store[0];
double rec_junk;
//double global_max;
MPI_Status status;
if(rank==0)
{
MPI_Send(&send_junk, 4, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD); // send data to process 1
}
if(rank==1)
{
MPI_Recv(&rec_junk, 4, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status); // receive data from process 0
}
//check between process 0 and process 1 maxima
if(rec_junk>=max_store[1])
{
rec_junk = max_store[0];
}
else
{
rec_junk = max_store[1];
}
send_junk = rec_junk;
MPI_Send(&send_junk, 4, MPI_DOUBLE, 2, 0, MPI_COMM_WORLD); // send data to process 2
if(rank==2)
{
MPI_Recv(&rec_junk, 4, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status); // receive data from process 1
}
//check between process 1 and process 2 maxima
if(rec_junk>=max_store[2])
{
rec_junk = rec_junk;
}
else
{
rec_junk = max_store[2];
}
send_junk = rec_junk;
MPI_Send(&send_junk, 4, MPI_DOUBLE, 3, 0, MPI_COMM_WORLD); // send data to process 3
if(rank==3)
{
MPI_Recv(&rec_junk, 4, MPI_DOUBLE, 2, 0, MPI_COMM_WORLD, &status); // receive data from process 2
}
//check between process 2 and process 3 maxima
if(rec_junk>=max_store[3])
{
rec_junk = rec_junk;
}
else
{
rec_junk = max_store[3];
}
printf("global ring max = %f", rec_junk);
MPI_Finalize(); // programs should always perform a "graceful" shutdown
return 0;
}
我有问题:
我可以轻松打印和查看进程 ID 和局部最大值,但如何将局部最大值存储在单个数组中?
使用环形拓扑寻找全局最大值的更有效方法是什么?
非常欢迎您的建议。谢谢
最佳答案
要将所有进程的本地结果收集到一个数组中,如果您希望它们都在进程 0 中,请使用 MPI_Gather
,或者在每个进程中获取它们时使用 MPI_Allgather
。
关于c++ - 来自环形拓扑的全局最大值,而不是来自 C++ 中的 MPI_REDUCE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623595/
我对 MPI 很陌生,我正在尝试使用 MPI_Reduce 来查找整数数组的最大值。我有一个整数数组 arr尺寸arraysize ,这是我的代码: MPI_Init(&argc, &argv); M
我在 fortran 中遇到 MPI_REDUCE() 的精度问题。我测试了两种对存储在每个节点上的 double 数字求和的方法。我使用的 MPI_REDUCE() 行是 call MPI_REDU
只是一个问题。如果我使用函数 MPI_Reduce,只有根可以分配接收缓冲区,当这是一个动态数组时?。例如: int r = 10; int *yloc, *y; ... yloc = calloc(
我正在尝试使用 MPI_reduce() 计算跨进程分布的 1000 个数字的平均值。不幸的是,到目前为止,我的代码只给出零,而不是预期的答案(数字的算术平均值)。 我做错了什么? #include
MPI documentation断言接收缓冲区 (recvbuf) 的地址中的地址仅在根目录下才有意义。这意味着可能不会在其他进程中分配内存。 this question 证实了这一点. int M
我正在尝试在 C(或 C++)中使用 MPI_REDUCE 来查找数组中的最小值(和索引)。我有这个适用于常规数组,但现在我想切换到一个动态计算值的数组。即 arr[n] 的值是通过调用 fun(n)
我开始研究使用 C 和 OpenMPI 库进行并行编程。所以现在一切看起来都太复杂了。 我正在尝试做一个单程序多数据:掌握:- 初始化一个数组- segmentation 它- 将相同大小的位发送到不
我最近遇到了一个堰行为。如果我在我的机器上运行以下代码(使用最新版本的 cygwin,Open MPI 版本 1.8.6),我会得到一个线性增长的内存使用量,这很快就会淹没我的电脑。 program
我试图将我的处理器分成几组,然后添加每组的总和独立地......但直到现在我还无法正确找到结果。一个简单的例子如下: int main(int argc, char** argv) { int
我得到了一个小练习,我必须通过使用 MPI 来估计 n 个球体的总体积来实现蒙特卡罗算法,这些球体的中心坐标和半径在 3 维中。即使我们必须使用 MPI,我们也可以在本地机器上启动所有进程,因此没有网
我正在尝试找到随机生成的数字的最大值。对此有任何想法... 我正在使用 MPI_Scatter 将随机生成的数字分成相等的进程。我正在使用 MPI_Reduce 从每个进程中获取最大值。 #inclu
我正在尝试做一些简单的 MPI 项目(w MPICH),但在这样做时我遇到了一个我既不理解也无法解决的问题(可能是因为我误解了文档)。所以我基本上想做的是将一个结构传递给 MPI_Reduce,以便对
我的 MPI 代码有问题,当代码在多个节点 上运行时挂起。它在单个节点 上运行时成功 完成。我不确定如何调试它。有人可以帮我调试这个问题吗? 程序使用: mpicc -o string strin.c
我有一个顺序程序,它计算数值并将结果存储在一个数组中,其中每个元素代表一个数字“位置”。 例如,一个计算结果[2,4,5,1,1,8,9,3,1]表示一个十进制值2.45118931 格式是resul
当使用 MPI_reduce 时,根处理器是否也对自身应用指定的 MPI 操作? 例如,假设以下代码由包括 root 在内的所有处理器运行,root 是否将它的 local_sum 减少为 globa
我学会了使用一些 MPI 函数。当我尝试使用 MPI_Reduce 时,我在运行代码时收到检测到堆栈粉碎: #include #include #include void main(int ar
我有两个程序。产生执行一些计算的“ worker ”的“主人”,我希望主人从 worker 那里得到结果并存储总和。我正在尝试使用 MPI_Reduce 从 worker 那里收集结果,而 worke
我不明白为什么一旦我使用包含动态分配数组的自定义 MPI 数据类型,MPI_Reduce() 就会出现段错误。有人知道吗 ?以下代码在 MPI_Reduce() 内部使用 2 个处理器时会崩溃。但是,
我正在尝试使用 MPI 库用 C 语言编写程序。在我的程序中,我正在解决 TSP(但没有使用任何特殊算法......)。我的输入参数是int nCites , int xCoord[]和 int yC
我需要缩减节点从其他节点获取元素列表的拷贝(存储在 vector 中)。我定义了自己的还原函数,但它不起作用。程序终止/崩溃。 这是代码: #include #include "mpi.h" #in
我是一名优秀的程序员,十分优秀!