ᴢᴡᴘ 2021-11-04 04:01 采纳率: 0%
浏览 86

C语言将串行程序改为MPI并行

代码如下


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
    int n;//The number of random numbers generated
    int a[1000] = {0};//[1000] indicates that there are 1000 0s in the array a[]
    int b[1000];
    int loop_times;//loop_times means the times of loop
    printf("Please enter the length of the array n: ");
    scanf("%d",&n);//Use scanf to enter the random number n that needs to be generated

    for ( loop_times = 0; pow(2,loop_times) <= n; loop_times++){}//for loop: in order to calculate the maximum quadratic power in n
    loop_times--;//Subtract the result by 1, and get the power of 2 at the end interval of the up phase
            srand((unsigned)time(NULL));//Initialize random seed
            for (int i = 0; i < n; i++)//For loop randomly generate n random numbers and assign them to a[0],a[1],...,a[n-1]
                {
                    a[i] = rand()%20;//Limit the generated random number to less than 20
                    b[i] = a[i];//Let b[i] = a[i]
                }
                printf("\n");
    int x;//x used to limit the power of 2
    int interval;//interval means the step
    int middle_number;//"middle_number"means the number used for calculation in the formula
    if (n & (n - 1)){//if n is not the power of 2
        printf("\nUp Phase\n");
        for(int i = 0; i < pow(2, loop_times + 1);i++)//for loop:print the random array b[i]
                printf("%5d ", b[i]);//For beauty, each number occupies 5 digits so that each row of numbers can be aligned
                printf("\n");
        for(x = 1; x <= loop_times + 1; x++){//for loop:limit the array no bigger than loop_times + 1,and fill in the missing digits with 0
            interval = pow(2, x);//interval means step
            middle_number = pow(2, x - 1);//middle_number always equal to the interval divided by 2(according to the law in the example).
            for(int i = 0; i < pow(2, loop_times + 1); i++)//for loop:make a calculation
                if (i % interval == interval - 1)//Let the formula be calculated only in a specified number of digits
                    b[i] = b[i - middle_number] + b[i];//The formula
                    for (int i = 0; i < pow(2, loop_times + 1); i++)//for loop:print the result
                    {
                        printf("%5d ", b[i]);
                    }
                    printf("\n");
        }//End of the up phase
        printf("\nDown Phase\n");
        for (int i = 0; i < pow(2, loop_times + 1); i++)//for loop: print the result of up phase again
                {
                    printf("%5d ", b[i]);
                }
                printf("\n");
        for (x = 0; x < loop_times + 1; x++){
            interval = pow(2, loop_times + 1 - x);
            middle_number = pow(2, loop_times - x);
            for(int i = 0; i < pow(2, loop_times + 1); i++){
                if(i % interval == interval - 1){
                    b[i + middle_number] = b[i + middle_number] + b[i];
                }
            }
                for (int i = 0; i < pow(2, loop_times + 1); i++)//for loop
                {
                    printf("%5d ", b[i]);
                }
                printf("\n");
        }
    }
    else{//if n is the power of 2
        printf("\nUp Phase\n");
        for(int i = 0; i < n;i++)//for loop:print the random array b[i]
                printf("%5d ", b[i]);
                printf("\n");
        for(x = 1; x < loop_times + 1; x++){
            interval = pow(2, x);
            middle_number = pow(2, x - 1);
            for(int i = 0; i < n; i++)
                if (i % interval == interval - 1)
                    b[i] = b[i - middle_number] + b[i];
                    for (int i = 0; i < n; i++)
                        printf("%5d ", b[i]);
                        printf("\n");
                    }
                    printf("\n");
        }
        printf("\nDown Phase\n");

        for (x = 0; x < loop_times + 1; x++){
            interval = pow(2, loop_times + 1 - x);
            middle_number = pow(2, loop_times - x);
            for(int i = 0; i < n; i++){
                if(i % interval == interval - 1){
                    b[i + middle_number] = b[i + middle_number] + b[i];
                }
            }
                for (int i = 0; i < n; i++)
                {
                    printf("%5d ", b[i]);
                }
                printf("\n");
        }
    return 0;//Complete this function successfully
}

  • 写回答

1条回答 默认 最新

  • Linux猿 Linux领域优质创作者 2021-11-04 08:19
    关注

    MPI有对应函数,把代码放到函数下,记得加头文件!
    PS:有问题可以再问我,学习数据结构和算法、C/C++、Linux可以关注我。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月4日

悬赏问题

  • ¥15 关于Finetune模型,CUDA error: device-side assert triggered 报错
  • ¥15 能将阿里云上多个设备的信息能上传给小程序吗
  • ¥50 QT6.7 Camera预览窗口,camera分辨率设置
  • ¥15 车机是安卓4.4.3,车机没有gps,我想用外置蓝牙gps。所以在用安卓模拟位置服务时候,我下载的相关软件不显示在列表里
  • ¥15 matlab水位控制系统(详解)
  • ¥15 CST软件仿真,已知中心线方程构建图形
  • ¥15 supLink 用户定位
  • ¥15 materials studio中sorption模块得到的吸附量如何计算出绝对吸附量和自由气量
  • ¥15 odoo17生产成品入出库会计日记账设置及自动产生会计分录
  • ¥15 MCU控制20V PWM波输出的控制电路