Ntinakos

ParallelNBody

Nov 10th, 2025
118
0
360 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | Software | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package parallelnbody;
  7.  
  8. import java.awt.Color;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.util.Locale;
  13. import java.util.Scanner;
  14.  
  15. /**
  16.  *
  17.  * @author alefrag
  18.  */
  19. public class ParallelNBody {
  20.  
  21.     //Set value to simulate <execTime> seconds of the bodies
  22.     static final int execTime = 100;
  23.  
  24.     static final boolean draw = false;
  25.    
  26.     public static void main(String[] args) throws FileNotFoundException {
  27.          //Make sure we are at the correct format for input PA
  28.         Locale.setDefault(new Locale("en", "US"));
  29.         //Allow for reading from args[0] PA
  30.         String fname = args[0];
  31.         int numThreads = 1; // default
  32.         if (args.length > 1) {
  33.             try {
  34.                 numThreads = Integer.parseInt(args[1]);
  35.                 if (numThreads < 1) numThreads = 1;
  36.             } catch (NumberFormatException e) {
  37.                 numThreads = 1;
  38.             }
  39.         }
  40.         java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(numThreads);
  41.         FileInputStream is = new FileInputStream(new File(fname));
  42.         System.setIn(is);
  43.        
  44.         Scanner console = new Scanner(System.in);
  45.  
  46.         final double dt = 0.1;                     // time quantum
  47.         int N = console.nextInt();                 // number of particles
  48.         double radius = console.nextDouble();      // radius of universe
  49.  
  50.         // turn on animation mode and rescale coordinate system PA
  51.         if (draw) {
  52.             StdDraw.show(0);
  53.             StdDraw.setXscale(-radius, +radius);
  54.             StdDraw.setYscale(-radius, +radius);
  55.         }
  56.         // read in and initialize bodies
  57.         Body[] bodies = new Body[N];               // array of N bodies
  58.         for (int i = 0; i < N; i++) {
  59.             double px = console.nextDouble();
  60.             double py = console.nextDouble();
  61.             double vx = console.nextDouble();
  62.             double vy = console.nextDouble();
  63.             double mass = console.nextDouble();
  64.             int red = console.nextInt();
  65.             int green = console.nextInt();
  66.             int blue = console.nextInt();
  67.             Color color = new Color(red, green, blue);
  68.             bodies[i] = new Body(px, py, vx, vy, mass, color);
  69.         }
  70.  
  71.         // Store time after IO
  72.         long startTime = System.nanoTime();
  73.  
  74.         // simulate the universe
  75.         for (double t = 0.0; t < execTime; t = t + dt) {
  76.  
  77.             Quad quad = new Quad(0, 0, radius * 2);
  78.             BHTree tree = new BHTree(quad);
  79.  
  80.             // build the Barnes-Hut tree (serial)
  81.             for (int i = 0; i < N; i++) {
  82.                 if (bodies[i].in(quad)) {
  83.                     tree.insert(bodies[i]);
  84.                 }
  85.             }
  86.  
  87.             // parallel update: χωρίζουμε τα bodies σε chunks, ένα Runnable ανά thread
  88.             java.util.List<java.util.concurrent.Callable<Void>> tasks = new java.util.ArrayList<>(numThreads);
  89.             final int chunkSize = (N + numThreads - 1) / numThreads; // ceil
  90.             for (int ti = 0; ti < numThreads; ti++) {
  91.                 final int start = ti * chunkSize;
  92.                 final int end = Math.min(N, start + chunkSize);
  93.                 tasks.add(new java.util.concurrent.Callable<Void>() {
  94.                     @Override
  95.                     public Void call() throws Exception {
  96.                         for (int i = start; i < end; i++) {
  97.                             bodies[i].resetForce();
  98.                             tree.updateForce(bodies[i]); // read-only on tree
  99.                             bodies[i].update(dt);
  100.                         }
  101.                         return null;
  102.                     }
  103.                 });
  104.             }
  105.  
  106.             try {
  107.                 // invokeAll θα μπλοκάρει μέχρι να ολοκληρωθούν όλα τα tasks
  108.                 executor.invokeAll(tasks);
  109.             } catch (InterruptedException ex) {
  110.                 Thread.currentThread().interrupt();
  111.                 break;
  112.             }
  113.  
  114.             // If draw is enabled update picture
  115.             if (draw) {
  116.                 StdDraw.clear(StdDraw.BLACK);
  117.                 for (int i = 0; i < N; i++) {
  118.                     bodies[i].draw();
  119.                 }
  120.                 StdDraw.show(0);
  121.             }
  122.         }
  123.  
  124.         //Terminating the executor
  125.         executor.shutdown();
  126.         try {
  127.             if (!executor.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) {
  128.                 executor.shutdownNow();
  129.             }
  130.         } catch (InterruptedException ex) {
  131.             executor.shutdownNow();
  132.             Thread.currentThread().interrupt();
  133.         }
  134.  
  135.         //Print execution time and exit
  136.         long endTime = System.nanoTime();
  137.         long totalTime = endTime - startTime;
  138.         System.out.println("Simulation time: " + ((double) totalTime) / 1E9);
  139.         System.exit(0);
  140.     }
  141.    
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment