-
大小: 12KB文件類型: .cpp金幣: 1下載: 0 次發布日期: 2021-06-12
- 語言: C/C++
- 標簽: MPI??parallel_com??
資源簡介
并行計算實驗使用MPI計算N體問題代碼,命令:mpiexec -n 1 EXP3.exe 64 10 1 g。
代碼片段和文件信息
//?EXP3.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include?
#include?
#include?
#include?
#include?
//#define?OUTPUT
//#define?DEBUG
#define?DIM?????2
#define?X???????0
#define?Y???????1
typedef?double?vect_t[DIM];
const?double?G?=?6.67e-11;
int?my_rank?comm_sz;?//進程號?總進程數
MPI_Datatype?vect_mpi_t;????//用于發送
MPI_Datatype?cyclic_mpi_t;??//?環形傳輸的數據類型??用于接收
vect_t?*vel?=?NULL;
vect_t?*pos?=?NULL;?????????//?位置信息變成了全局變量
void?Usage(char*?prog_name)
{
fprintf(stderr?“usage:?mpiexec?-n??%s\n“?prog_name);
fprintf(stderr?“????\n“);
fprintf(stderr?“????‘g‘:?inite?condition?by?random\n“);
fprintf(stderr?“????‘i‘:?inite?condition?from?stdin\n“);
exit(0);
}
void?Get_args(int?argc?char*?argv[]?int*?n_p?int*?timestep?int*?output_freq_p?char*?g_i_p)
{
if?(my_rank?==?0)
{
if?(argc?!=?5)
Usage(argv[0]);
*n_p?=?strtol(argv[1]?NULL?10);
*timestep?=?strtol(argv[2]?NULL?10);
*output_freq_p?=?strtol(argv[3]?NULL?10);
*g_i_p?=?argv[4][0];
if?(*n_p?<=?0?||?*n_p?%?comm_sz?||?*timestep?0?||?*g_i_p?!=?‘g‘?&&?*g_i_p?!=?‘i‘)
{
printf(“haha\n“);
if?(my_rank?==?0)
Usage(argv[0]);
MPI_Finalize();
exit(0);
}
}
MPI_Bcast(n_p?1?MPI_INT?0?MPI_COMM_WORLD);
MPI_Bcast(timestep?1?MPI_INT?0?MPI_COMM_WORLD);
MPI_Bcast(output_freq_p?1?MPI_INT?0?MPI_COMM_WORLD);
MPI_Bcast(g_i_p?1?MPI_CHAR?0?MPI_COMM_WORLD);
#???ifdef?DEBUG
printf(“Get_args?rank%2d?n?%d?n_steps?%d?output_freq?%d?g_i?%c\n“
my_rank?*n_p?*timestep?*output_freq_p?*g_i_p);
fflush(stdout);
#???endif
}
void?Build_cyclic_mpi_type(int?loc_n)//?生成大小為?loc_n?的循環分配數據類型
{
MPI_Datatype?temp_mpi_t;
MPI_Aint?lb?extent;
MPI_Type_vector(loc_n?1?comm_sz?vect_mpi_t?&temp_mpi_t);//?將跨度為?comm_sz?的?loc_n?個?“連續?2?個?double”??封裝為?temp_mpi_t
MPI_Type_get_extent(vect_mpi_t?&lb?&extent);
MPI_Type_create_resized(temp_mpi_t?lb?extent?&cyclic_mpi_t);
MPI_Type_commit(&cyclic_mpi_t);
}
void?Gen_init_cond(double?masses[]?vect_t?loc_pos[]?vect_t?loc_vel[]?int?n?int?loc_n)
{
const?double?mass?=?1.0e4?gap?=?1.0e-2?speed?=?0;
if?(my_rank?==?0)
{
//?srand(2);
for?(int?i?=?0;?i? {
masses[i]?=?mass;
pos[i][X]?=?i?*?gap;
pos[i][Y]?=?0.0;
vel[i][X]?=?0.0;
//?vel[i][Y]?=?speed?*?(2?*?rand()?/?(double)RAND_MAX)?-?1);
vel[i][Y]?=?(i?%?2)???-speed?:?speed;
}
}
MPI_Bcast(masses?n?MPI_DOUBLE?0?MPI_COMM_WORLD);//從一個序列號為root的進程將一條消息廣播發送到組內的所有進程這里是將n體初始化信息廣播
MPI_Scatter(pos?1?cyclic_mpi_t?loc_pos?loc_n?vect_mpi_t?0?MPI_COMM_WORLD);//?loc_pos?和?loc_vel?需要分別分發
MPI_Scatter(vel?1?cyclic_mpi_t?loc_vel?loc_n?vect_mpi_t?0?MPI_COMM_WORLD);//每個進程獲取自己的loc_pos和loc_vel
#???ifdef?DEBUG
printf(“Gen_init_cond?rank%2d?mase[0]%10.3e?posX%10.3e?posY%10.3e?velX%10.3e?v
- 上一篇:64位的GDIPLUS庫
- 下一篇:數據結構--家譜管理系統內含報告
評論
共有 條評論