OpenMP环境配置
MPI环境配置
欧拉黎曼函数的K阶近似OpenMP
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include<time.h>
#include <windows.h>
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 += 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++)
{
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));
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]