gpt4 book ai didi

linux - 在 MPI 集群上运行 C 程序

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:06 26 4
gpt4 key购买 nike

我在由两个节点组成的集群上运行 MPI 程序(用 C 或 C++ 编写)时遇到问题。细节:操作系统:Ubuntu 16.04节点数:2(主从)

一切正常。当我在集群上运行一个简单的 mpi_hello 程序并将 12 作为参数(进程数)时,我看到 4 个 mpi-hello 实例在从属节点上运行(使用检查顶部)。

Output on master node + mpi_hello instances running on the second node (slave node)当我尝试运行另一个程序(例如一个计算和打印范围内质数的简单程序)时,它正在主节点上运行,但我在从节点上看不到它的任何实例。

#include <stdio.h>
#include<time.h>
//#include</usr/include/c++/5/iostream>
#include<mpi.h>



int main(int argc, char **argv)
{
int N, i, j, isPrime;
clock_t begin = clock();

int myrank, nprocs;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

printf("Hello from the processor %d of %d \n" , myrank, nprocs);


printf("To print all prime numbers between 1 to N\n");
printf("Enter the value of N\n");
scanf("%d",&N);

/* For every number between 2 to N, check
whether it is prime number or not */
printf("Prime numbers between %d to %d\n", 1, N);

for(i = 2; i <= N; i++){
isPrime = 0;
/* Check whether i is prime or not */
for(j = 2; j <= i/2; j++){
/* Check If any number between 2 to i/2 divides I
completely If yes the i cannot be prime number */
if(i % j == 0){
isPrime = 1;
break;
}
}

if(isPrime==0 && N!= 1)
printf("%d ",i);
}

clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("\nThe time spent by the program is %f\n" , time_spent);

while(1)
{}

MPI_Finalize();

return 0;

}

背后可能的原因是什么?还有其他方法可以检查它是否也在从属节点上运行吗?谢谢

最佳答案

好的,这是我使用的代码。包含前 500 个整数的向量。现在我想将它们平均分成 4 个进程(即每个进程获得 125 个整数——第一个进程获得 1-125,第二个进程获得 126-250,依此类推)。我尝试使用 MPI_Scatter()。但我没有看到数据平均分配甚至分配。我是否必须使用 MPI_Recv() (我有另一段代码可以正常工作并且仅使用分散来平均划分数据)。你能找出代码中的任何问题吗?谢谢

int main(int argc, char* argv[])
{
int root = 0;

MPI_Init(&argc, &argv);

int myrank, nprocs;

MPI_Status status;


//variables for prime number calculation
int num1, num2, count, n;


MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

char name[MPI_MAX_PROCESSOR_NAME + 1];
int namelen;

MPI_Get_processor_name(name, &namelen);




cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;



int size = 500;
int size1 = num2 / nprocs;

cout << "The size of each small vector is " << size1 << endl;

auto start = get_time::now(); //start measuring the time


vector<int> sendbuffer(size), recbuffer(size1); //vectors/buffers involved in the processing


cout << "The prime numbers between " << num1 << " and " << num2 << " are: " << endl;

if (myrank == root)
{
for (unsigned int i = 1; i <= num2; ++i) //array containing all the numbers from which you want to find prime numbers
{
sendbuffer[i] = i;
}

cout << "Processor " << myrank << " initial data";
for (int i = 1; i <= size; ++i)
{
cout << " " << sendbuffer[i];

}
cout << endl;




MPI_Scatter(&sendbuffer.front(), 125, MPI_INT, &recbuffer.front(), 125, MPI_INT, root, MPI_COMM_WORLD);
}



cout << "Process " << myrank << " now has data ";
for (int j = 1; j <= size1; ++j)
{

cout << " " << recbuffer[j];

}
cout << endl;



auto end = get_time::now();
auto diff = end - start;
cout << "Elapsed time is : " << chrono::duration_cast<ms>(diff).count() << " microseconds " << endl;


MPI_Finalize();
return 0;
}`

关于linux - 在 MPI 集群上运行 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39272317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com