Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.7 kB
2
Indexable
Never
public static void main(String[] args) {
        MPI.Init(args);
        int rank = MPI.COMM_WORLD.Rank();
        int size = MPI.COMM_WORLD.Size();

        length = Integer.parseInt(args[3]);
        numberOfCycles = Integer.parseInt(args[4]);

        int startIndex = rank * (length / size);
        int endIndex = (rank + 1) * (length / size);
        if(rank == size - 1){
            endIndex = length;
        }

        Particle[] particles = new Particle[endIndex-startIndex];

        for (int i = 0; i < endIndex-startIndex; i++)
            particles[i] = new Particle();          //creating particles*/


        MPI.COMM_WORLD.Barrier();

        double startTime = System.currentTimeMillis();

        Particle[] currentParticleArr = new Particle[1];
        Particle currentParticle;

        for(int cycle = numberOfCycles; cycle > 0; cycle--){
            for (int i = 0; i < length; i++) {
                if(i >= startIndex && i < endIndex)
                    currentParticleArr[0] = particles[i % (endIndex-startIndex)];

                MPI.COMM_WORLD.Bcast(currentParticleArr, 0, 1, MPI.OBJECT, rank == size - 1 ? Math.min(size - 1, i / (length / size)) : Math.min(size - 1, i / (endIndex-startIndex)));

                currentParticle = currentParticleArr[0];

                updateParticles(particles, currentParticle);

                if(i >= startIndex && i < endIndex)
                    checkBoundaries(currentParticle);
            }
        }
        if(rank == 0) {
            double endTime = System.currentTimeMillis();
            System.out.println("Time: " + (endTime - startTime) / 1000 + "s");
        }
        MPI.Finalize();
        System.exit(0);

    }