最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

。。。

2023-05-26 20:10 作者:釋o懷灬  | 我要投稿

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

#include <mpi.h>


double f(double x) {

? ? return 4.0 / (1.0 + x * x);

}


int main(int argc, char **argv) {

? ? int rank, size, i, n;

? ? double pi, h, sum, x;


? ? MPI_Init(&argc, &argv);


? ? MPI_Comm_rank(MPI_COMM_WORLD, &rank);

? ? MPI_Comm_size(MPI_COMM_WORLD, &size);


? ? if (argc != 2) {

? ? ? ? if (rank == 0) {

? ? ? ? ? ? printf("Usage: mpiexec -n <num_procs> %s <num_steps>\n", argv[0]);

? ? ? ? }

? ? ? ? MPI_Finalize();

? ? ? ? return 1;

? ? } else {

? ? ? ? n = atoi(argv[1]);

? ? }


? ? // 梯形積分法

? ? double start_time_trap = MPI_Wtime();? // 記錄開始時間

? ? h = 1.0 / n;

? ? int chunk = n / size;


? ? int start = rank * chunk;

? ? int end = (rank == size - 1) ? n : (rank + 1) * chunk;


? ? sum = 0.0;

? ? for (i = start; i < end; i++) {

? ? ? ? x = h * (i + 0.5);

? ? ? ? sum += f(x);

? ? }

? ? sum *= h;


? ? MPI_Reduce(&sum, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); // 結(jié)果匯總


? ? if (rank == 0) {

? ? ? ? double end_time_trap = MPI_Wtime();? // 記錄結(jié)束時間

? ? ? ? printf("Using trapezoidal rule with %d processes, %d steps, pi = %.16f, time = %f s\n", size, n, pi, end_time_trap - start_time_trap);

? ? }


? ? // 蒙特卡洛法

? ? int total_samples = atoi(argv[1]) / size;


? ? double start_time_monte = MPI_Wtime();? // 記錄開始時間

? ? srand(time(NULL) + rank * 1000);


? ? count = 0;

? ? for (i = 0; i < total_samples; i++) {

? ? ? ? x = (double)rand() / RAND_MAX;

? ? ? ? y = (double)rand() / RAND_MAX;

? ? ? ? if (x * x + y * y <= 1.0) {

? ? ? ? ? ? count++;

? ? ? ? }

? ? }


? ? int global_count;

? ? MPI_Reduce(&count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);? // 結(jié)果匯總


? ? if (rank == 0) {

? ? ? ? double end_time_monte = MPI_Wtime();? // 記錄結(jié)束時間

? ? ? ? pi = 4.0 * (double)global_count / (double)atoi(argv[1]);

? ? ? ? printf("Using Monte Carlo method with %d processes, %d samples, pi = %.16f, time = %f s\n", size, atoi(argv[1]), pi, end_time_monte - start_time_monte);

? ? }


? ? MPI_Finalize();

? ? return 0;

}


。。。的評論 (共 條)

分享到微博請遵守國家法律
蕉岭县| 双峰县| 庆城县| 房山区| 佛教| 淮滨县| 阳新县| 年辖:市辖区| 阳朔县| 弥渡县| 盐亭县| 北流市| 茌平县| 靖安县| 和林格尔县| 伊宁市| 准格尔旗| 夏津县| 东海县| 大同县| 永城市| 夏河县| 额敏县| 清原| 久治县| 陇南市| 花垣县| 清水河县| 霍林郭勒市| 华容县| 汾西县| 根河市| 沙河市| 洛隆县| 汪清县| 遂宁市| 大兴区| 大英县| 凤凰县| 新安县| 黑水县|