- MPI学习笔记
- C++中数组不能赋值, a = {1, 2} error, 因为a是一个指针, 不能把{1,2}赋值给一个指针
- 并行排序使用Send / Recv
#include <stdio.h> #include <mpi.h> #include <sys/time.h> void sort(int* a, int n) { int temp; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } int main(int argc, char * argv[]) { int a[10000], b[5000]; for (int i = 0; i < 10000; i++) { a[i] = 10000 - i; } int rank, size; struct timeval start, end; long total_time; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (rank == 0) { gettimeofday(&start, nullptr); MPI_Send(&a[5000], 5000, MPI_INT, 1, 0, MPI_COMM_WORLD); sort(a, 5000); MPI_Recv(b, 5000, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); for (int i = 5000; i < 10000; i++) { a[i] = b[i - 5000]; } sort(a, 1000); gettimeofday(&end, nullptr); total_time = (1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec)) / 1000; printf("size : %d time is %ld\n", size, total_time); } if (rank == 1) { MPI_Recv(b, 5000, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); sort(b, 500); MPI_Send(b, 5000, MPI_INT, 0, 0, MPI_COMM_WORLD); } MPI_Finalize(); return 0; }
- c语言动态分配数组
- vector pell(18); 是定义一个长度为18的,就是包含18个元素的数组。
pell不是元素地址,是vector对象。
&pell实际上就是取到了pell 数组对象的地址。并不是数组的首地址。
[并行与分布式程序设计] MPI第二讲笔记:并行排序使用Send / Recv
猜你喜欢
转载自blog.csdn.net/weixin_40996518/article/details/106253822
今日推荐
周排行