/*
 * $Id: Space2PE.java,v 1.3 1999/08/04 15:21:10 yamauchi Exp $
 *     (M\pi)^2 : Allocate Spaces to Processing Elements 
 *	Copyright 1999 By YAMAUCHI Hitoshi 山内 斉 
 *	@author YAMAUCHI Hitoshi 山内 斉
 *	
 * original source spc2PE.h spc2PE.cc
 *
 *	部分空間と PE のマッピング
 * 
 * 1999年8月 3日(火)
 */

package jp.ac.tohoku.is.archi.mpi2.common;

import  jp.ac.tohoku.is.archi.mpi2.util.IntVec3;
import  jp.ac.tohoku.is.archi.mpi2.util.Assert;

public abstract class Space2PE {
    protected int mSystemDimension;
    protected int [] mNumberOfPE; 	// [X, Y, Z]
    protected int [] mNumberOfSPCof1PE;	// 1 つの PE の持つ空間数
    protected int mSubDivNum;	
    protected IntVec3 mPEID;

    public String baseString()
    {
	String s = "Space2PE\n" + 
	    "system dimension " + this.mSystemDimension + "\n" +
	    "number of PEs    (" + this.mNumberOfPE[0]  + " " +
	    this.mNumberOfPE[1] + " " + this.mNumberOfPE[2] + ")" + "\n" +
	    "number of space of 1PE ("+ this.mNumberOfSPCof1PE[0] + " " +  
	    this.mNumberOfSPCof1PE[1] + " " + this.mNumberOfSPCof1PE[2]
	    + ")\n" +
	    "space subdivision " + this.mSubDivNum + "\n" +
	    "PEID " + this.mPEID;
	return(s);
    }
    
    public Space2PE()
    {
	this.mSystemDimension 	= 0;
	this.mNumberOfPE 	= null;
	this.mNumberOfSPCof1PE	= null;
	this.mSubDivNum 	= -1;
	this.mPEID		= null;
    }
    
    public abstract IntVec3 getNewPEIDfromSpcPos(IntVec3 spcPos);
    // local Idx = 物体情報を担当空間分のみとした場合の PE 内部の index
    public abstract IntVec3 getNewLocalIdxFromSpcPos(IntVec3 spcPos);

    /**
     * PE の次元を知る
     */
    public static int getSystemDimension(IntVec3 PEnum)
    {
	int dimension = 3;		// PE 数が一つの軸の数を引いていく
	for(int i = 0; i < 3; i++)
	{
	    Assert.out(PEnum.get(i) > 0, ("Illegal PEnum" + PEnum.toString()));
	    if(PEnum.get(i) == 1)
	    {
		dimension--;
	    }
	}
	if(dimension == 0)	// 全ての方向に 1 つづつ
	{
	    dimension = 1;		// 1 つしか PE がない
	}
	if((dimension < 1) || (3 < dimension))
	{
	    throw new RuntimeException ("Error! : Illegal PE dimension "
					+ "at getSystemDimension");
	    // commMpi2_exit(1);
	}
	return(dimension);
    }
}
