zoukankan      html  css  js  c++  java
  • 选择排序的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;
    }

  • 相关阅读:
    Service Name Port Number Transport Protocol tcp udp 端口号16bit
    linux linux 互传文件 win 不通过 ftp sftp 往linux 传文件(文件夹)
    soft deletion Google SRE 保障数据完整性的手段
    Taylor series
    Taylor's theorem
    Moving average
    REQUEST
    Unix file types
    mysqld.sock
    Tunneling protocol
  • 原文地址:https://www.cnblogs.com/beautiful-code/p/6416701.html
Copyright © 2011-2022 走看看