递归任务,完成其工作

时间:2014-02-25 21:50:41

标签: java recursion

我试图创建一个递归任务,将一组元素分成更小的部分并对它们运行一系列数学工作,比如数字积分,然后尝试为每个元素使用单独的结果。但似乎在池完全运行之前发生错误。我的代码试图到达尚未处理的元素。 你能帮帮我吗?

public void ComputeStiffnessMatrix(DataScanner DS){

    ans=new double [2*(DS.getXarray().length)][2*(DS.getXarray().length)];

    int nelels=((DS.getNelKsi())*(DS.getNelEta()));

    ArrayList<ElementStiffness> elsm=new ArrayList<>();

    StiffnessMatrix SM=new StiffnessMatrix(elsm,DS,0,(nelels-1));
    ForkJoinPool pool = new ForkJoinPool();
    System.out.println("Stiffness Matrix Computation...");
    long startTime = System.currentTimeMillis();
    pool.invoke(SM);
    long endTime = System.currentTimeMillis();

    System.out.println("Stiffness Matrix Computation took " + (endTime - startTime) +  " milliseconds.");
    System.out.println("Arranging into an array...");
    long startTime2 = System.currentTimeMillis();



    try {
        ArrayList<ElementStiffness> kk=(ArrayList<ElementStiffness>) SM.get();

        for (int el=0;el<nelels;el++){

            ElementStiffness pp=kk.get(el);
            System.out.println("EL NO."+el);
            double vals[][]=pp.getStiffnessMatrix();
            int sup[]=pp.getControlpointsSupport();

            for (int i=0;i<sup.length;i++){
                for (int j=0;j<sup.length;j++){
                    ans[(2*sup[i])+0][(2*sup[j])+0]=ans[(2*sup[i])+0][(2*sup[j])+0]+vals[(2*i)+0][(2*j)+0];
                    ans[(2*sup[i])+0][(2*sup[j])+1]=ans[(2*sup[i])+0][(2*sup[j])+1]+vals[(2*i)+0][(2*j)+1];
                    ans[(2*sup[i])+1][(2*sup[j])+0]=ans[(2*sup[i])+1][(2*sup[j])+0]+vals[(2*i)+1][(2*j)+0];
                    ans[(2*sup[i])+1][(2*sup[j])+1]=ans[(2*sup[i])+1][(2*sup[j])+1]+vals[(2*i)+1][(2*j)+1];
                }
            }
        }


    } catch (InterruptedException | ExecutionException ex) {
        Logger.getLogger(IGATest.class.getName()).log(Level.SEVERE, null, ex);
    }
    long endTime2 = System.currentTimeMillis();

    System.out.println("Stiffness Matrix rearrangement took " + (endTime2 - startTime2) +  " milliseconds.");

    try{
        FileOutputStream fs=new FileOutputStream("stifmat.tmp");
        ObjectOutputStream os=new ObjectOutputStream(fs);
        os.writeObject(ans);
        os.close();
    }catch (IOException e){
        e.printStackTrace();
    }

    ans=null;


}

siffnessmatrix课程:

public class StiffnessMatrix扩展了RecursiveTask {

private final int min;
private final int max;
private final DataScanner ds;
ArrayList<ElementStiffness> elsm;

public StiffnessMatrix (ArrayList<ElementStiffness> elsm,DataScanner DS,int min, int max){

    this.ds=DS;
    this.min=min;
    this.max=max;
    this.elsm=elsm;
}



protected void compdir(){

    for (int i=min;i<=max;i++){
        double[] elsp=ds.getElementSpan(i);
        Element el=new Element(ds,((elsp[0]+elsp[1])*0.5),((elsp[2]+elsp[3])*0.5));
        ElementStiffness els=new ElementStiffness(ds,el);
        elsm.add(els);
    }
}

@Override
protected ArrayList <ElementStiffness> compute(){


    int processors = Runtime.getRuntime().availableProcessors();
    if (max-min<processors) {      
        compdir();
    } else {
        int center = min + (max - min) / 2;
        invokeAll(new StiffnessMatrix(elsm,ds, min, center) , new StiffnessMatrix(elsm, ds, center+1, max));

    }     
    return elsm;
}

}

0 个答案:

没有答案