2016-09-01 110 views
0

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

一切正常。当我和12簇作为参数上运行一个简单mpi_hello程序(没有。的过程)我看到(选中使用顶部)4 从节点上运行的MPI-你好实例。当我尝试运行另一个程序(例如计算和打印范围中的素数的简单程序)时,它在主节点上运行,但在从节点上看不到它的任何实例。

#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; 

} 

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

+0

我不会感到惊讶,这是由于'stdin'没有连接到所有MPI进程。试着摆脱'scanf()'并将该值作为命令行参数传递。 – Gilles

+0

感谢吉尔,我现在对程序中的值进行了硬编码。它显示在第二个节点上工作。但我没有看到任何优势。与使用MPI并行运行时相比,在不使用MPI的情况下在单个节点上运行相同的程序更快(占用一半时间)。也许我没有正确实施并行。你能指出代码中的任何问题吗?代码与上面相同......感谢 – shaibi

+0

没有工作共享到您的代码中:您不会将工作分配到MPI进程中;所有流程都涉及到整个工作。因此,没有机会获得任何加速。 – Gilles

回答

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; 
}`