在MPI集群上运行C程序

时间:2016-09-01 13:04:47

标签: linux mpi cluster-computing ubuntu-16.04

在通过包含两个节点的群集运行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;

}

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

1 个答案:

答案 0 :(得分: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;
}`