#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
void generate_diamond_part(int size, int start_row, int end_row, char* buffer) {
int half_size = size / 2;
int line_idx, space_count, star_count, buffer_idx = 0;
for (line_idx = start_row; line_idx < end_row; ++line_idx) {
space_count = abs(half_size - line_idx);
star_count = size - space_count * 2;
memset(buffer + buffer_idx, ' ', space_count);
buffer_idx += space_count;
memset(buffer + buffer_idx, '#', star_count);
buffer_idx += star_count;
memset(buffer + buffer_idx, ' ', space_count);
buffer_idx += space_count;
buffer[buffer_idx++] = '\n';
}
buffer[buffer_idx] = '\0';
}
int main(int argc, char* argv[]) {
int rank, size, i;
int ARRAY_SIZE = 4001; // Make sure this is an odd number
int line_length = ARRAY_SIZE + 1; // Including newline
char* diamond_part;
char* full_diamond = NULL;
int* recv_counts;
int* displacements;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
double start_time = MPI_Wtime();
int lines_per_proc = ARRAY_SIZE / size;
int remaining_lines = ARRAY_SIZE % size;
int start = lines_per_proc * rank + ((rank < remaining_lines) ? rank : remaining_lines);
int lines_this_proc = lines_per_proc + ((rank < remaining_lines) ? 1 : 0);
int end = start + lines_this_proc;
// Allocate buffer for local diamond part
diamond_part = malloc(lines_this_proc * line_length * sizeof(char));
generate_diamond_part(ARRAY_SIZE, start, end, diamond_part);
// Gather all parts of the diamond at the root process
if (rank == 0) {
// Only root process holds the entire diamond
full_diamond = malloc(ARRAY_SIZE * line_length * sizeof(char));
recv_counts = malloc(size * sizeof(int));
displacements = malloc(size * sizeof(int));
for (i = 0; i < size; ++i) {
int lines_proc = lines_per_proc + ((i < remaining_lines) ? 1 : 0);
recv_counts[i] = lines_proc * line_length;
displacements[i] = (i < remaining_lines) ? (i * (lines_per_proc + 1) * line_length) : (remaining_lines * (lines_per_proc + 1) * line_length + (i - remaining_lines) * lines_per_proc * line_length);
}
}
MPI_Barrier(MPI_COMM_WORLD); // 同步所有进程
// Gather all parts of the diamond at the root process
MPI_Gatherv(diamond_part, lines_this_proc * line_length, MPI_CHAR,
full_diamond, recv_counts, displacements, MPI_CHAR,
0, MPI_COMM_WORLD);
// 计时结束
MPI_Barrier(MPI_COMM_WORLD); // 同步所有进程
double end_time = MPI_Wtime();
// 打印结果
if (rank == 0) {
// Root process prints the full diamond twice
full_diamond[ARRAY_SIZE * line_length - 1] = '\0'; // Remove last newline
printf("First diamond:\n%s\n", full_diamond);
printf("Second diamond:\n%s\n", full_diamond);
}
// 打印运行时间
if (rank == 0) {
printf("Runtime: %f seconds\n", end_time - start_time);
}
// Clean up resources
free(diamond_part);
if (rank == 0) {
free(full_diamond);
free(recv_counts);
free(displacements);
}
MPI_Finalize();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
高性能计算:实验作业及代码
共4个文件
c:4个
2 下载量 141 浏览量
2023-12-20
17:46:43
上传
评论
收藏 3KB RAR 举报
温馨提示
高性能计算:实验作业及代码
资源推荐
资源详情
资源评论
收起资源包目录
demo.rar (4个子文件)
demo
mpi_sum.c 1KB
mpi_diamond.c 3KB
diamond.c 970B
c_sum.c 497B
共 4 条
- 1
资源评论
m0_60688831
- 粉丝: 100
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功