OpenMP环境配置222

发布时间:2024年01月22日

OpenMP环境配置

MPI环境配置

欧拉黎曼函数的K阶近似OpenMP

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>

#include<time.h>
#include <windows.h>        //window环境下调用Sleep()函数包含此头文件

double zeta_approximation(int s, int k)
{
    double result = 0.0;

#pragma omp parallel for reduction(+:result)
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++)
        {
            //result += 1.0 / (pow(n, k) * pow(k, n));
            result += pow(-1, i+1) / pow(i+j, s);
        }
    }

    return result * pow(2, s);
}

int main()
{
    int s = 20;
    int k = 3000;

	clock_t start = 0;
	clock_t end   = 0;
    double approximation = 0.0;

    start = clock();
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= k; j++)
        {
            //result += 1.0 / (pow(n, k) * pow(k, n));
            approximation += pow(-1, i + 1) / pow(i + j, s);
        }
    }
    approximation = approximation * pow(2, s);
    end = clock();
    printf("zeta(%d, %d) ≈ %f\n", s, k, approximation);
    printf("总的cpU时间 = %f\n", (end - start) / (double)CLOCKS_PER_SEC);


    omp_set_num_threads(4);  // 设置线程数量
    approximation = 0.0;
    start = 0;
    end = 0;

	start = clock();
    approximation = zeta_approximation(s, k);
	end = clock();
    printf("Zetaop(%d, %d) ≈ %f\n", s, k, approximation);
    printf("总的CPU时间 = %f\n", (end - start) / (double)CLOCKS_PER_SEC);

    return 0;
}

uuuu

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#include <time.h>

double zeta_approximation(int s, int k, int rank, int num_procs)
{
    double result = 0.0;
    int start = rank * (k / num_procs) + 1;
    int end = (rank + 1) * (k / num_procs);

    if (rank == num_procs - 1) {
        end = k;
    }

    for (int i = start; i <= end; i++) {
        for (int j = 1; j <= k; j++) {
            result += pow(-1, i + 1) / pow(i + j, s);
        }
    }

    return result;
}

int main(int argc, char** argv)
{
    int s = 20;
    int k = 3000;
    double approximation = 0.0;
    double total_approximation = 0.0;
    int rank, num_procs;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

    double start_time = MPI_Wtime();

    approximation = zeta_approximation(s, k, rank, num_procs);

    MPI_Reduce(&approximation, &total_approximation, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

    double end_time = MPI_Wtime();

    if (rank == 0) {
        printf("Zetaop(%d, %d) ≈ %f\n", s, k, total_approximation * pow(2, s));
        printf("总的CPU时间 = %f\n", end_time - start_time);
    }

    MPI_Finalize();

    return 0;
}

欧拉黎曼函数的K阶近似MPI

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>

double zeta_approximation(int s, int k, int rank, int num_procs)
{
    double result = 0.0;
    int start = rank * (k / num_procs) + 1;
    int end = (rank + 1) * (k / num_procs);

    if (rank == num_procs - 1) {
        end = k;
    }

    for (int i = start; i <= end; i++) {
        for (int j = 1; j <= k; j++) {
            result += pow(-1, i + 1) / pow(i + j, s);
        }
    }

    return result;
}

int main(int argc, char** argv)
{
    int s = 20;
    int k = 800;
    double approximation = 0.0;
    double total_approximation = 0.0;
    int rank, num_procs;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

    double start_time = MPI_Wtime();

    approximation = zeta_approximation(s, k, rank, num_procs);

    MPI_Reduce(&approximation, &total_approximation, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

    double end_time = MPI_Wtime();

    if (rank == 0) {
        printf("Zetaop(%d, %d) ≈ %f\n", s, k, total_approximation * pow(2, s));
        printf("总的CPU时间 = %f\n", end_time - start_time);
    }

    MPI_Finalize();

    return 0;
}

生命游戏(Game of life)

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

#define M 10  // 行数
#define N 10  // 列数

int count_alive_neighbors(int* grid, int row, int col)
{
    int count = 0;

    // 检查相邻细胞的生死状态
    for (int i = row - 1; i <= row + 1; i++) {
        for (int j = col - 1; j <= col + 1; j++) {
            if (i >= 0 && i < M && j >= 0 && j < N && !(i == row && j == col)) {
                count += grid[i * N + j];
            }
        }
    }

    return count;
}

void update_grid(int* old_grid, int* new_grid)
{
#pragma omp parallel for collapse(2)
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            int alive_neighbors = count_alive_neighbors(old_grid, i, j);

            if (old_grid[i * N + j]) {
                // 活细胞的规则
                if (alive_neighbors < 2 || alive_neighbors > 3) {
                    new_grid[i * N + j] = 0;  // 死亡
                }
                else {
                    new_grid[i * N + j] = 1;  // 存活
                }
            }
            else {
                // 死细胞的规则
                if (alive_neighbors == 3) {
                    new_grid[i * N + j] = 1;  // 繁殖为活细胞
                }
                else {
                    new_grid[i * N + j] = 0;  // 保持死亡
                }
            }
        }
    }
}

void print_grid(int* grid)
{
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", grid[i * N + j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main()
{
    int* grid     = (int*)malloc(M * N * sizeof(int));
    int* new_grid = (int*)malloc(M * N * sizeof(int));

    // 初始化细胞状态,随机填充0和1
    for (int i = 0; i < M * N; i++) {
        grid[i] = rand() % 2;
    }

    printf("初始状态:\n");
    print_grid(grid);

    int max_steps = 10;  // 最大迭代次数

    for (int step = 0; step < max_steps; step++) {
        update_grid(grid, new_grid);

        printf("第 %d 回合:\n", step + 1);
        print_grid(new_grid);

        // 将新的数组代替旧数组
        int* temp = grid;
        grid = new_grid;
        new_grid = temp;
    }

    free(grid);
    free(new_grid);

    return 0;
}

参考资料

[1]
[2]
[3]

文章来源:https://blog.csdn.net/weixin_46191137/article/details/135744090
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。