-
为什么在windows下打开压缩包按照说明运行run_simple_demo终端会一直出现无限循环
-
埃拉托斯特尼筛法(sieve of Eratosthenes),简称埃氏筛,也称素数筛,是简单且历史悠久的筛法,用来找出一定范围内所有素数。在寻找整数N以内的素数时,古希腊数学家埃拉托斯特尼采用了一种与众不同的方法:先将2-N的各数写在纸上:在2的上面画一个圆圈,然后划去2的其他倍数;第一个既未画圈又没有被划去的数是3,将它画圈,再划去3的其他倍数;现在既未画圈又没有被划去的第一个数是5,将它画圈,并划去5的其他倍数……依此类推,一直到所有小于或等于N的各数都画了圈或划去为止。这时,画了圈的以及未划去的那些数正好就是小于N的素数。 接下来贴出详细代码: #include "mpi.h" #include "math.h" #include "stdio.h" #include "stdlib.h" #include "string.h" #include "error.h" using namespace std; #define BLOCK_LOW(id, p, n) ((id) * (n) / (p)) #define BLOCK_HIGH(id, p, n) (BLOCK_LOW((id) + 1, p, n) - 1) #define BLOCK_SIZE(id, p, n) (BLOCK_LOW((id) + 1) - BLOCK_LOW(id)) #define BLCOK_OWNER(index, p, n) (((p)* (index) +1 ) -1 / (n)) #define MIN(a, b) ((a)<(b)?(a):(b)) int main(int argc, char *argv[]) { int count; /* Local prime count */ double elapsed_time; /* Parallel execution time */ int first; /* Index of first multiple */ int global_count; /* Global prime count */ int high_value; /* Highest value on this proc */ int id; /* Process ID number */ int index; /* Index of current prime */ int low_value; /* Lowest value on this proc */ char *marked; /* Portion of 2,...,'n' */ int n; /* Sieving from 2, ..., 'n' */ int p; /* Number of processes */ int proc0_size; /* Size of proc 0's subarray */ int prime; /* Current prime */ int size; /* Elements in 'marked' */ int low_index; /* Lowest index on this proc */ int high_index; /* Highest index on this proc */ MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Comm_size(MPI_COMM_WORLD, &p); MPI_Barrier(MPI_COMM_WORLD); elapsed_time = -MPI_Wtime(); if (argc != 2) { if (!id) printf("Command line: %s <m> \n", argv[0]); MPI_Finalize(); exit(1); } n = atoi(argv[1]); int N = n - 1; low_index = id * (N / p) + MIN(id, N % p); high_index = (id + 1) * (N / p) + MIN(id + 1, N % p) - 1; low_value = 2 + low_index; high_value = 2 + high_index; size = high_value - low_value + 1; proc0_size = (n - 1) / p; if ((2 + proc0_size) < (int) sqrt((double) n)) { if (!id) printf("Too many processes \n"); MPI_Finalize(); exit(1); } marked = (char *) malloc(size); if (marked == NULL) { printf("Cannot allocate enough memory \n"); MPI_Finalize(); exit(1); } for (int i = 0; i < size; i++) marked[i] = 0; if (!id) index = 0; prime = 2; do { if (prime * prime > low_value) { first = prime * prime - low_value; } else { if (!(low_value % prime)) first = 0; else first = prime - (low_value % prime); } for (int i = first; i < size; i += prime) marked[i] = 1; if (!id) { while (marked[++index]); prime = index + 2; } if (p > 1) { MPI_Bcast(&prime, 1, MPI_INT, 0, MPI_COMM_WORLD); } } while (prime * prime <= n); count = 0; for (int i = 0; i < size; i++) if (marked[i] == 0) { count++; } MPI_Reduce(&count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); elapsed_time += MPI_Wtime(); if (!id) { printf("%d primes are less than or equal to %d \n", global_count, n); printf("Total elapsed time: %10.6f\n", elapsed_time); } MPI_Finalize(); if (!id){ char filename[50] = "result.txt"; FILE *fp; if ((fp = fopen(filename,"a+")) == NULL){ printf("fail to open file"); exit(0); } fprintf(fp, "%d %d %10.6f\n", p, n, elapsed_time); fclose(fp); } return 0; }
推荐直播
-
华为云码道-玩转OpenClaw,在线养虾2026/03/11 周三 19:00-21:00
刘昱,华为云高级工程师/谈心,华为云技术专家/李海仑,上海圭卓智能科技有限公司CEO
OpenClaw 火爆开发者圈,华为云码道最新推出 Skill ——开发者只需输入一句口令,即可部署一个功能完整的「小龙虾」智能体。直播带你玩转华为云码道,玩转OpenClaw
回顾中 -
华为云码道-AI时代应用开发利器2026/03/18 周三 19:00-20:00
童得力,华为云开发者生态运营总监/姚圣伟,华为云HCDE开发者专家
本次直播由华为专家带你实战应用开发,看华为云码道(CodeArts)代码智能体如何在AI时代让你的创意应用快速落地。更有华为云HCDE开发者专家带你用码道玩转JiuwenClaw,让小艺成为你的AI助理。
回顾中 -
Skill 构建 × 智能创作:基于华为云码道的 AI 内容生产提效方案2026/03/25 周三 19:00-20:00
余伟,华为云软件研发工程师/万邵业(万少),华为云HCDE开发者专家
本次直播带来两大实战:华为云码道 Skill-Creator 手把手搭建专属知识库 Skill;如何用码道提效 OpenClaw 小说文本,打造从大纲到成稿的 AI 原创小说全链路。技术干货 + OPC创作思路,一次讲透!
回顾中
热门标签