• 选择排序的MPI实现


    #include "stdafx.h"
    #include "mpi.h"
    #include <stdio.h>
    #include <math.h>
    #include <windows.h>
    #include "stdlib.h"
    #define SIZE 2//进程数
    const int ARRAY_SIZE = 100000/SIZE;//每个进程分配的个数
    int shuzu[SIZE][ARRAY_SIZE];
    int zonghanshu[SIZE][ARRAY_SIZE];
    double endwtime;
    double startwtime;
    void swap(int *a, int *b);
    int main(int argc, char *argv[]){
        int myid;
        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    
        int numtasks;
        srand(10086);
        for (int i = 0; i < SIZE; i++){
            for (int j = 0; j < ARRAY_SIZE; j++){
                shuzu[i][j] = rand();
            }
        }
    
        int xiaopaixu[ARRAY_SIZE];
    
        startwtime = MPI_Wtime();
        MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
        if (numtasks == SIZE){
            MPI_Scatter(shuzu, ARRAY_SIZE, MPI_INT, xiaopaixu, ARRAY_SIZE, MPI_INT, 0, MPI_COMM_WORLD);
    
    
    
            for (int i = 0; i <ARRAY_SIZE; i++){
                for (int j = i + 1;j < ARRAY_SIZE ;  j++){
                    if (xiaopaixu[i] > xiaopaixu[j]){
                        swap(&xiaopaixu[i],&xiaopaixu[j]);
    
                    }
                }
            }
    
            MPI_Gather(xiaopaixu, ARRAY_SIZE, MPI_INT, zonghanshu, ARRAY_SIZE, MPI_INT, 0, MPI_COMM_WORLD);
    
            int time[SIZE];
            for (int i = 0; i < SIZE; i++){
                time[i] = 0;
            }
            int a[SIZE];
            int zongpaixu2[ARRAY_SIZE*SIZE];
            for (int j = ARRAY_SIZE*SIZE - 1; j >= 0; j--){
                for (int k = 0; k < SIZE; k++){
                    if (time[k] >= ARRAY_SIZE){
                        a[k] = 0;
                    }
                    else
                    {
                        a[k] = zonghanshu[k][ARRAY_SIZE - time[k] - 1];
                    }
                }
    
                int x = a[0];
                for (int i = 1; i<SIZE; i++){
                    if (a[i]>x){
                        x = a[i];
                    }
                }
                for (int n = 0; n < SIZE; n++){
                    if (x == a[n]){
                        time[n] = time[n] + 1;
                        break;
                    }
                }
    
                zongpaixu2[j] = x;
            }
    
            endwtime = MPI_Wtime();
    
            if (myid);
            else
                for (int i = 0; i < SIZE*ARRAY_SIZE; i++){
                    printf("%d ", zongpaixu2[i]);
                }
    
        }
    
        if (myid);
        else
            printf("
    time=%f
    ", endwtime - startwtime);
        MPI_Finalize();
    
    
    }
    
    void swap(int *a, int *b)
    {
        int  c;
        c = *a;
        *a = *b;
        *b =  c;
    }

  • 相关阅读:
    Module not found: Error: Can't resolve './style':配置 extensions 的坑
    Mysql5.7前后修改用户密码变化
    一步步分析Java深拷贝的两种方式clone和序列化
    Windows下Nodejs的开发环境搭建
    JavaNIO第一话Buffer
    Vscode浏览器打开html vscode修改默认浏览器
    Windows电脑多个SSH Key管理.md
    jsonp跨域的原理
    POJ1502(MPI Maelstrom)
    POJ1088(滑雪)
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/6416701.html
Copyright © 2020-2023  润新知