首页 > 其他分享 >利用MPI并行计算任意范围内的质数

利用MPI并行计算任意范围内的质数

时间:2024-06-11 16:03:40浏览次数:14  
标签:int 质数 number MPI 并行计算 include

#include<stdio.h>
#include<mpi.h>
#include<malloc.h>
#include<math.h>
#include<string.h>

bool jud(int a) {
	int k = 0;
	if (a <= 1) 
		return false;
	for (int i = 2; i < pow(a, 0.5) + 1; i++) {
		k = a % i;
		if (k == 0) {
			return false;
		}
	}
	return true;
}

int main(int argc, char* argv[])
{
	int rank, size;
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	int tot = 10000;     //判断上限
	int times = tot / size;
	int exc = tot % size;
	int num = 0;
	int number = 0;
	int temp_num = 0;

	for (int i = 0; i < times; i++) {
		temp_num = rank + size * i;
		if (jud(temp_num)) {
			num++;
		}
	}

	if (rank < exc) {
		int part = 0;
		part = size * times + rank + 1;
		if (jud(part)) {
			num++;
		}
	}

	MPI_Reduce(&num, &number, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
	if (rank == 0) {
		if (tot >= 1) {
			number++;
		}
		printf("%d", number);
	}
	MPI_Finalize();
	return 0;
}

标签:int,质数,number,MPI,并行计算,include
From: https://blog.csdn.net/qq_68898943/article/details/139601405

相关文章

  • COMPILER simplified C programmin
    COMPILERPROJECT2024Thegoaloftheterm-projectistoimplementabottom-upsyntaxanalyzer(a.k.a.,parser)aswe’velearned.Morespecifically,youwillimplementthesyntaxanalyzerforasimplifiedCprogramminglanguagewiththefollowingcontextf......
  • C++ MPI多进程并发
    下载用法mpiexec-n8$PROCESS_COUNTx64\Debug\$TARGET.exe 多进程并发启动mpiexec-fhosts.txt-n3$PROCESS_COUNTx64\Debug\$TARGET.exe  联机并发进程,其它联机电脑需在相同路径下有所有程序//hosts.txt192.168.86.16192.168.86.123192.168.86.108De......
  • JAVA stringcompiler动态编译
    packagecompiler.mydemo;importjavax.tools.Diagnostic;importjavax.tools.DiagnosticCollector;importjavax.tools.FileObject;importjavax.tools.ForwardingJavaFileManager;importjavax.tools.JavaCompiler;importjavax.tools.JavaFileManager;importjava......
  • C++ 6.8笔记:①判断质数②二分基础算法
    质数试除法判定质数boolprimes(intx){  if(x<2)returnfalse;  for(inti=2;i<=x/i;i++){    if(x%i==0)returnfalse;  }  returntrue;}埃筛1intp[N],k,n;boolf[N];voidprimes(intn){//埃筛,思想:质数的倍数是合数for(inti......
  • mask -rcnn benchmark编译失败RuntimeError: Error compiling objects for extension(
    1.首先几步跟官网一样condacreate--namemaskrcnn_benchmarkpython==3.8(建议装3.8,好装pytorch)condaactivatemaskrcnn_benchmark#thisinstallstherightpipanddependenciesforthefreshpythoncondainstallipythonpip#maskrcnn_benchmarkandcocoa......
  • ERROR Failed to compile with 1 error
    解决方法一:重新运行:npmrunserve(每个人情况不定)解决方法二:可能是文件中有中文名,将该项目文件名称及该项目文件的上一层命名为纯英文。重新:npmrunserve解决方法三:修改相关的 webpack 配置文件把 index.html 文件重命名为 index.ejs 文件在 node_nodul......
  • go tool compile 报错 could not import sync (file not found)
    前言Go版本:$goversiongoversiongo1.21.4darwin/amd64我想对go文件进行反汇编,然后就报错了:$gotoolcompile-Srace.gorace.go:3:8:couldnotimportsync(filenotfound)我就惊讶了一下,标准库怎么还能找不到呢?难道是我GOROOT配置错了?发现了问题原因原......
  • AndroidStudio升级Gradle到7+,compileSdkVersion 33+
    一、概述由于需求方的要求/需要,主动或被动的需要升级android的编译环境到CompileSdkVersion33。此时直接更改android项目的编译版本会报错,as版本过低或者gradle插件太老了等。也会遇到一些这样那样的bug,这一篇做一下简单的总结升级方式:以更......
  • MPI(二)- 进程调度,绑定
    单节点情况下不显式绑定CPU核心MPI运行时环境会依赖操作系统来管理MPI进程与CPU核心的映射和调度。操作系统会尝试均匀分配负载,但可能会出现缓存污染、上下文切换开销增加以及NUMA访问延迟等问题。默认调度操作系统的默认调度器会将进程分配到可用的CPU核心上,尝试均匀分......
  • Error in system(paste(MAKE, p1(paste("-f", shQuote(makefiles))), "compilers"),
     001、R语言windows中安装R包出现如下报错Errorinsystem(paste(MAKE,p1(paste("-f",shQuote(makefiles))),"compilers"),:'make'notfound 002、确认是否安装Rtoolsinstall.packages("pkgbuild")pkgbuild::find_rtools(debug=TRU......