在java中的main之外初始化MPI
Posted
技术标签:
【中文标题】在java中的main之外初始化MPI【英文标题】:Initializing MPI outside of main in java 【发布时间】:2020-01-05 02:16:36 【问题描述】:我试图在 java 中创建一个分布式 MPI 类,它将为我做一些计算。不幸的是, MPI.Init 似乎需要主类中的 argv,argc。有没有办法在这个类中初始化 MPI?
public class distributed
public distributed(int mat[][],int n,int m) throws MPIException
MPI.Init(argv); // issue is here,i dont have "argv" to initialize with,causing null pointer exceptions in variables like "myrank"
final int myrank = MPI.COMM_WORLD.Rank();
final int size = MPI.COMM_WORLD.Size();
final int rows = n;
final int rowChunk = (rows+size-1)/size;
final int startRow = myrank *rowChunk;
int endRow = (myrank+1)*rowChunk;
int[] newRow = new int[m];
【问题讨论】:
【参考方案1】:为什么不在 main 方法中初始化 myrank
和 size
。然后将它们作为Distributed
类构造函数的参数传递?
public class Distributed
public Distributed(int mat[][],int n,int m, int myrank, int size)
// These two variables are now parameters of the constructor
// final int myrank = MPI.COMM_WORLD.Rank();
// final int size = MPI.COMM_WORLD.Size();
final int rows = n;
final int rowChunk = (rows+size-1)/size;
final int startRow = myrank *rowChunk;
int endRow = (myrank+1)*rowChunk;
int[] newRow = new int[m];
public static void main (String [] args)
MPI.Init(args, args.length);
final int myrank = MPI.COMM_WORLD.Rank();
final int size = MPI.COMM_WORLD.Size();
Distributed d = new Distributed (..., myrank, size);
【讨论】:
以上是关于在java中的main之外初始化MPI的主要内容,如果未能解决你的问题,请参考以下文章