Visual studio 在VisualBasic2008Pro中使用MPI_RECV和MPI_SEND挂起MPI并行计算
我正在尝试在Windows7(64位)下的HP Intel Core i7 860单台计算机中使用并行计算。 由于我将进行并行计算的代码使用C语言以及其他一些原因,我使用Visual Studio 2008 Professional Edition,方法是设置[Project Properties Debugger to Launch:Local Windows Debugger Command:C:\Program Files\Microsoft MPI\Bin\mpiexec.exe和命令参数:-n 8“$(TargetPath)”] 我已安装Microsoft MPI(4.2.4400.0)并使用mpiexec.exe。当我运行下面的示例并行代码()时,出现了一个问题。 黑色执行窗口弹出,但挂起,不显示任何预期消息,如“sum%i由根进程计算\n”。我只能通过Ctrl+C退出正在运行的程序。 我已经通过键入mpiexec.exe-np 8 xxx.exe从windowcmd运行了该程序,但仍然存在挂起问题Visual studio 在VisualBasic2008Pro中使用MPI_RECV和MPI_SEND挂起MPI并行计算,visual-studio,mpi,mpiexec,Visual Studio,Mpi,Mpiexec,我正在尝试在Windows7(64位)下的HP Intel Core i7 860单台计算机中使用并行计算。 由于我将进行并行计算的代码使用C语言以及其他一些原因,我使用Visual Studio 2008 Professional Edition,方法是设置[Project Properties Debugger to Launch:Local Windows Debugger Command:C:\Program Files\Microsoft MPI\Bin\mpiexec.exe和命令参
#include <stdio.h>
#include <mpi.h>
#define max_rows 100000
#define send_data_tag 2001
#define return_data_tag 2002
int array[max_rows];
int array2[max_rows];
main(int argc, char **argv)
{
long int sum, partial_sum;
MPI_Status status;
int my_id, root_process, ierr, i, num_rows, num_procs,
an_id, num_rows_to_receive, avg_rows_per_process,
sender, num_rows_received, start_row, end_row, num_rows_to_send;
/* Now replicate this process to create parallel processes.
* From this point on, every process executes a seperate copy
* of this program */
ierr=MPI_Init(&argc, &argv);
root_process=0;
/* Find out My process ID, and how many processes were started. */
ierr=MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr=MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
if(my_id==root_process){
/* I must be the root process, so I will query the user
* to determine how many numbers to sum. */
//printf("please enter the number of numbers to sum: ");
//scanf("%i", &num_rows);
num_rows=100;
//if(num_rows>max_rows){
// printf("Too manay numbers.\n");
// exit(1);
//}
avg_rows_per_process=num_rows/num_procs;
/* Initialize an array */
for(i=0; i<num_rows; i++){
array[i]=i+1;
}
/* Distribute a portion of the vector to each child process */
for(an_id=1; an_id<num_procs; an_id++){
start_row=an_id*avg_rows_per_process+1;
end_row=(an_id+1)*avg_rows_per_process;
if((num_rows-end_row)<avg_rows_per_process)
end_row=num_rows-1;
num_rows_to_send=end_row-start_row+1;
ierr = MPI_Send( &num_rows_to_send, 1 , MPI_INT,
an_id, send_data_tag, MPI_COMM_WORLD);
ierr = MPI_Send( &array[start_row], num_rows_to_send, MPI_INT,
an_id, send_data_tag, MPI_COMM_WORLD);
}
/* and calculate the sum of the values in the segment assigned
* to the root process */
sum=0;
for(i=0; i<avg_rows_per_process+1;i++){
sum += array[i];
}
printf("sum %i calculated by root process\n", sum);
/* and, finally, I collect the partial sums from the slave processes,
* print them, and add them to the grzand sum, and print it */
for(an_id = 1; an_id < num_procs; an_id++) {
ierr = MPI_Recv( &partial_sum, 1, MPI_LONG, MPI_ANY_SOURCE,
return_data_tag, MPI_COMM_WORLD, &status);
sender = status.MPI_SOURCE;
printf("Partial sum %i returned from process %i\n", partial_sum, sender);
sum += partial_sum;
}
printf("The grand total is: %i\n", sum);
}
else {
/* I must be a slave process, so I must receive my array segment,
* storing it in a "local" array, array1. */
ierr = MPI_Recv( &num_rows_to_receive, 1, MPI_INT,
root_process, send_data_tag, MPI_COMM_WORLD, &status);
ierr = MPI_Recv( &array2, num_rows_to_receive, MPI_INT,
root_process, send_data_tag, MPI_COMM_WORLD, &status);
num_rows_received = num_rows_to_receive;
/* Calculate the sum of my portion of the array */
partial_sum = 0;
for(i = 0; i < num_rows_received; i++) {
partial_sum += array2[i];
}
/* and finally, send my partial sum to hte root process */
ierr = MPI_Send( &partial_sum, 1, MPI_LONG, root_process,
return_data_tag, MPI_COMM_WORLD);
}
getchar();
ierr = MPI_Finalize();
}
#包括
#包括
#定义最大行数100000
#定义发送数据标签2001
#定义返回数据标签2002
int数组[最大行数];
int array2[最大行数];
主(内部argc,字符**argv)
{
长整数和,部分整数和;
MPI_状态;
int my_id,root_进程,ierr,i,num_行,num_进程,
一个id,num-rows-to-receive,avg-rows-per-process,
发送方、接收的行数、开始行、结束行、发送的行数;
/*现在复制此过程以创建并行过程。
*从这一点开始,每个进程都执行一个单独的副本
*这个节目的*/
ierr=MPI_Init(&argc,&argv);
根进程=0;
/*找出我的进程ID,以及启动了多少进程*/
ierr=MPI通信等级(MPI通信世界和我的id);
ierr=MPI_通信大小(MPI_通信世界和进程数量);
if(my_id==根进程){
/*我必须是根进程,因此我将查询用户
*确定要求和的数字数*/
//printf(“请输入要求和的数字:”);
//scanf(“%i”行和num_行);
行数=100;
//如果(数量行>最大行){
//printf(“太多的数字。\n”);
//出口(1);
//}
每进程平均行数=行数/进程数;
/*初始化数组*/
对于(i=0;这可能是一个愚蠢的问题,但是您是否尝试在终端窗口中按下任何键,或者在示例程序结束时清除getchar()
?否则代码没有任何挂起的理由。是的,我尝试在挂起时按下某个键,但它仍然挂起,而没有getchar()。感谢您对我的问题感兴趣。抱歉@JonathanDursi,它在没有getchar()的情况下工作。可能我对我为另一个代码所做的工作感到困惑。谢谢!
#include <stdio.h>
#include <mpi.h>
main(int argc, char **argv)
{
int ierr, num_procs, my_id;
ierr = MPI_Init(&argc, &argv);
/* find out MY process ID, and how many processes were started. */
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
printf("Hello world! I'm process %i out of %i processes\n", my_id, num_procs);
ierr = MPI_Finalize();
}