Java for loop into parallel - java

I am new to parallel processing and trying to learn it. I got an assignment for my university and i have to turn a serial "barnes-hut" algorithm into a parallel one.
I've been searching and i don't seem to find anything useful.
I've figured i need to parallel with max thread number 4
for (int i = 0; i < N; i++) {
double px = console.nextDouble();
double py = console.nextDouble();
double vx = console.nextDouble();
double vy = console.nextDouble();
double mass = console.nextDouble();
int red = console.nextInt();
int green = console.nextInt();
int blue = console.nextInt();
Color color = new Color(red, green, blue);
bodies[i] = new Body(px, py, vx, vy, mass, color);
}

Thing is: you are actually looking at more than just "adding some parallel" to your code.
You see, your code does:
read multiple values from some input source
a bit of processing
store the final "results" within an ordered data structure (that array)
You can't just do the above in parallel. Because when you use more than one thread, you lose control when exactly things happen.
In your case: you lose control about the order of "reading from input". Yes, when you as the other answer suggests, you will run into your first multi-threading problem, because that other code isn't thread safe, therefore you will end up with unexpected, unpredictable outcome!
Also note: your code is really not well suited for "running in parallel". Using multiple threads only "helps" when your workload benefits from the multiple threads. Reading sequential data from one file, to end up with objects within a sequential data structure ... isn't such a workload!
Thus the real answer is: step back. Multi-threading is really complicated. You can't learn that stuff by picking "random" code, and then trying to "add parallels" to it. Pick a good book, or tutorial ... and research the topic. Look at well written examples, and then see how those are handled. Then search for assignments that are meant to learn about multi-threading.
One starting point: the concurrency tutorials from oracle.

You can look at IntStream to parallel your for loop:
IntStream.range(0,N).parallel().forEach(i -> {
double px = console.nextDouble();
double py = console.nextDouble();
double vx = console.nextDouble();
double vy = console.nextDouble();
double mass = console.nextDouble();
int red = console.nextInt();
int green = console.nextInt();
int blue = console.nextInt();
Color color = new Color(red, green, blue);
bodies[i] = new Body(px, py, vx, vy, mass, color);
});
Bear in mind this is just an example for the for loop to get you started. This could lead to the wrong results and will need modification for what you're doing.
To look at documentation on the parallel of "Barnes-Hut" check out:
https://www.cs.vu.nl/ibis/papers/nijhuis_barnes_2004.pdf
It is old and not using latest java techniques to simplify some of the parallelism but should be able to lead you down the right path
There is also a Scala project that simulates it as well:
https://github.com/tadjik1/Barnes-Hut-Simulation

Related

Minimize portoflio return difference using ojAlgo

I have a dataset of daily prices for a portfolio (I do not have details about the constituents, simply the total daily price). My goal is to select n funds from a pool of funds and create a new portfolio with daily returns as close as possible compared to the original portfolio. I came up with the following formula to describe the problem:
with Yi the original portfolio returns, π the fund weights and X the fund returns.
Additionally, I defined 3 constraints:
To solve this I need to use ojAlgo. I tried the following but I almost instantly got stuck because of 2 issues.
public static void main(String[] [] args){
ExpressionsBasedModel model = new ExpressionsBasedModel();
float[] x1 = getDailyReturns("fund1");
float[] x2 = getDailyReturns("fund2");
float[] x3 = getDailyReturns("fund3");
Variable pi1 = new Variable("pi1");
Variable pi2 = new Variable("pi2");
Variable pi3 = new Variable("pi3");
List<Variable> variables = Arrays.asList(w1, w2, w3);
model.addVariables(variables);
Expression expr = model.addExpression("objective");
expr.setLinearFactor(pi1, x1);
expr.setLinearFactor(pi2, x2);
expr.setLinearFactor(pi3, x3);
expr.weight(1.0);
Optimisation.Result result = model.minimise();
}
First of all, I have X as a vector while .setLinearFactor can only use Number. Secondly I obviously missed something on how I should define the Expression.
Do you know where I could find examples helping me understanding how I am supposed to use the ExpressionBasedModel() for my problem?
Thank you

Java Library To Animated Movement Between Coordinates

I am working on an application that deals with moving objects from point A to point B in a 2D space. The job of the application is to animate that transition in a given number to steps (frames).
What I am currently doing is divide the distance by the number steps, hence creating a very linear and boring movement in a straight line:
int frames = 25;
int fromX = 10;
int toX = 20;
double step = (toX - fromX) / frames;
List<Double> values = new ArrayList<>();
int next = start;
for (int i = 0; i < frames; i++) {
values.add(next);
next += step;
}
As a first improvement - since my poor users have to look at this misery - I would like that to be an accelerated and decelerated movement starting slow, picking up speed, then getting slower again until arrival at the destination.
For that particular case, I could probably figure out the math somehow but in the end, I want to be able to provide more complex animations that would go beyond my capabilities as a mathematician ;) I have many of the capabilities of e.g. PowerPoint or iMovie in mind.
My ask is: Is there a library that would allow me to generated these sequences of coordinates? I found a few things but they where often tied to some Graphics object etc which I am not using. For me it's all about Lists of Doubles.

Java complex calculation from a file using threads (concurrency)

So I have text files that I can parse with the code I have and make the calculations for some atoms in space. Thing is I have to make the program parallel using threads and I don't know how I am supposed to pass the text file in every thread.
If I try to do something like double kx = console.nextDouble(); it doesn't recognize what console is because its Scanner object inside of main(). What am I missing?
(Note: it's not strictly necessary to use threads directly; I could use fork-join pool, etc.)
public class NBodyBH {
//Set value to simulate <execTime> seconds of the bodies
static final int execTime = 100;
static final boolean draw = false;
public static void main(String[] args) throws FileNotFoundException {
String fname = args[0];
FileInputStream is = new FileInputStream(new File(fname));
System.setIn(is);
Scanner console = new Scanner(System.in);
final double dt = 0.1; // time quantum
int N = console.nextInt(); // number of particles
double radius = console.nextDouble(); // radius of universe
//BELOW HERE IS THE PART I WANT TO MAKE PARALLEL
Body[] bodies = new Body[N]; // array of N bodies
for (int i = 0; i < N; i++) {
double px = console.nextDouble();
double py = console.nextDouble();
double vx = console.nextDouble();
double vy = console.nextDouble();
double mass = console.nextDouble();
int red = console.nextInt();
int green = console.nextInt();
int blue = console.nextInt();
Color color = new Color(red, green, blue);
bodies[i] = new Body(px, py, vx, vy, mass, color);
}
}
}
I believe what you're looking for is AsynchronousFileChannel. It was introduced in Java 7 for async operations on files.
Although, you're saying you'll run a complex calculation so maybe the time intensive task is the calculation itself instead of the file reading, you could read the file in your main thread and use parallelism to run the complex calculations.
List<Integer> calculationResults = bodies.parallelStream()
.map(doComplexCalculation)
.collect(toList())
In the example I'm assuming the calculation returns an Integer and that bodies is a Collection like a List, but adapt it as you see fit.
Passing the file to each thread sounds like a bad idea. Then you'd have to coordinate which thread reads which data, which would be unnecessarily complicated, or (more likely), each thread needs all the data, and making each thread re-read the file is inefficient.
First, read all the data. Then pass each thread the data it needs.
Don't assign your new FileInputStream to System.in. Just create a Scanner with it directly:
Scanner input = new Scanner(Paths.get(arg[0]));
...
List<Body> bodies = new ArrayList<>(N);
for (int i = 0; i < N; ++i) {
...
bodies.add(new Body(...));
}
for (int step = 0; step < 10; ++step) { /* Simulate 10 time steps, or whatever. */
List<Body> next = bodies.parallelStream() /* Here's your threads. */
.map(body -> step(body, dt, bodies))
.collect(Collectors.toList());
/* Render new state */
...
bodies = next; /* Throw away your old state. */
}
Here, step() is a function you need to write. If I've guessed what you are doing correctly, it will create a clone of the body, with a new position and velocity, based on the positions of the others. If you can modify the code of the Body class, this method should probably be a member of that class.
You must be careful not to modify the "current" bodies, but only create replacement for each. This upholds the "non-interference" requirement of Stream parallelism. Each time step will take O(N2) time to compute, since each new state of each body (N) depends on the state of every other body (× N).

passing user input to methods java

Hey guys i am a complete beginner in java and i am trying to write a program that goes this way:
painting company has determined that for every 115 square feet of wall
space, one gallon of paint and eight hours of labor will be required.
The company charges $18.00 per hour for labor.
Write a program that allows the user to enter the number of rooms to
be painted and the price of the paint per gallon. It should also ask for
the square feet of wall space in each room. The program should have
methods that return the following data:
The number of gallons of paint required
The hours of labor required
The cost of the paint
The labor charges
The total cost of the paint job
Then it should display the data on the screen
this is what i have so far. i can get the user input and the number of gallons but when i try to calculate the number of hours of labor what it does is it calculates the labor from the result of the previous calculation which is not what i want..this is what i have so far
int resultnumberofhours = hoursoflabor(numberofsquarefeet,115,8);
System.out.println("the number of hours needed are " + resultnumberofhours);
}
public static int hoursoflabor(int numberofsquarefeet, int squarefeet,int labor){
int resultnumberofhours = numberofsquarefeet/115*8;
return resultnumberofhours;
}
Rather then taking one by one you can try code like this
Scanner dd = new Scanner(System.in);
int[] vars = new int[5];
for(int i = 0; i < vars.length; i++) {
System.out.println("Enter next var: ");
vars[i] = dd.nextInt();
}
The only problem that I can see with the code is that you are not doing anything with the results. Try the following:
int result = numberofsquarefeet(numberofsquarefeet, 115, 1);
System.out.println("Number of square feet: " + result);
When returning something from a java function you can either assign that value to a variable of the same type you specify as the return type of the function, or you can use the output in a print statement for example. If you do nothing with the output, that result is lost.
Hope this helps.
You have to either print the result at the end:
System.out.println("enter number of square feet");
numberofsquarefeet = Keyboard.nextInt();
int resultSqFeet = numberofsquarefeet(numberofsquarefeet, 115, 1);
System.out.println("number of square feet is" + resultSqFeet);
}
or inside the methods:
public static int numberofsquarefeet(int numberofsquarefeet, int squarefeet, int paint) {
int result = numberofsquarefeet / 115 * 1;
System.out.println("number of square feet is " + result);
return result;
}
The same goes with hoursoflabor
I suggest you step back from writing the code and analyze your problem a bit first.
You have a data model and a process which you can describe in more detail. This will help you come up with a good design for your classes and methods. If you immediately start writing code, especially as a beginner, it usually ends up like a pile of spaghetti.
Here is my suggestion for the data model:
PaintingCompany
PAINT_GALLONS_PER_SQUARE_FOOT = 1 / 115
LABOR_HOURS_PER_SQUARE_FOOT = 8 / 115
LABOR_DOLLARS_PER_HOUR = 18
Room
wallSpaceSquareFeet
Paint
dollarsPerGallon
gallons
calculatePaintCostInDollars()
Project
rooms
paint
calculateGallonsOfPaintRequired()
calculateHoursOfLaborRequired()
calculateLaborChargesInDollars()
calculateTotalCostInDollars()
Note that the PaintingCompany class only contains constants. You could put the main() method in this class if you like.
The type and amount of paint are modeled as a single class 'Paint'. You could argue this is inaccurate and have a separate PaintType (enum) representing known types of paint and their prices but I figured this was feature creep at this point. So with the model above you need to instantiate a Paint instance with the correct price and then set the amount of paint on it. The Paint class will then have a function to calculate the total paint price.
It would be an idea to model the units explicitly. The model is now fixed on dollars and gallons but in future you might wish to switch to euros and liters in order to sell the application in Europe. For now I didn't model units because it probably over complicates the exercise. To avoid confusion it is important that the units are specified clearly as part of the variable or method name Space rockets have been known to crash due to errors of this genre.
The process could look like this:
Create a new Project
Ask the user for the paint price
Set the paint price on the project's Paint object
Start an iteration:
Add a new Room object to the Project
Ask the user for the wall space of the Room in square feet
Ask the user if there is another room
If yes, iterate from step 4
If no, print a report with the required project data
Note that some of these process steps are good candidates for separate classes:
Iteration could be put in the PaintingCompany class
Getting input from the user. It's good to keep this separate so you can easily change it later without affecting other code.
Printing the report. Reporting is usually a bit complex, has output formatting etc and is a nice separate concern. You also want to be able to change the report implementation without affecting other code.
If you post new code based on this (or your own) design I am happy to comment on it or help you with any questions.

Java programmming- help initialize(carpet)

I have started how to learn Java programming and I a got a problem while doing an asignment:
ps this is a c programming assignment, using it to do a java programming:
Consider a (potentially large commercial) room that has floor dimensions length len and width wid. The area of the floor is given by multiplying len by wid.
Carpet tiles are provided as individual squares (2 feet by 2 feet). Write, compile and run a C program, carpet.c, that
-calculates the minimum number of carpet tiles that are needed to cover a room whose dimensions (len and wid) are input from the terminal (measurements are in inches). NB: floor tiles can be cut to fit at the edges of the room - try to minimise waste.
-calculates how many carpet-tile packs are needed. The supplier only provides carpet tiles in packs of ten (i.e. individual tiles cannot be purchased separately).
-calculates how many spare tiles are left over once the minimum number of packs have been purchased.
-prints out all these results in a neat table along with the total cost of the order. (A pack of tiles costs $50 for each of the first four packs, and the price reduces to $45 for each subsequent pack.)
Think carefully about how are you going to test your program? The calculations are non-trivial and easy to get wrong. If your program doesn't work then you will waste the company a lot of money and you will probably lose the contract to supply the software.
You should write out a number of test cases (by hand) that cover all the different possibilities that could happen. Don't forget to consider various boundary cases too - these are often where errors are detected.
So far I have done:
import java.util.Scanner;
public class carpet {
public static void main (String args[]){
Scanner scanf = new Scanner (System.in);
float len, wid;
float area;
int roundTiles;
int roundPacks;
float tarea;
float tpack;
float NoOfTiles;
float NoOfPacks;
float tspares1;
float tspares2;
int packCost;
int cost;
tarea= 12* 12;
tpack= 10;
System.out.format("Enter the length of the room, Inches: ");
len = scanf.nextFloat();
System.out.format("Enter the width of the room, Inches: ");
wid = scanf.nextFloat();
area = len * wid;
NoOfTiles = area/ tarea;
NoOfPacks = NoOfTiles/tpack;
roundTiles = (int) Math.ceil(NoOfTiles);
roundPacks = (int) Math.ceil(NoOfPacks);
tspares1 = roundPacks * 10;
tspares2 = tspares1 - roundTiles;
if (roundPacks <= 4)
packCost =50;
else if(roundPacks > 4)
{
packCost = 45;
packCost = packCost + 20; *<<-----******LINE 50-----*********
}
cost =roundPacks * packCost; *<<*******---ERROR-------------*********
System.out.println(cost);
}
}
The error says: "The local variable packCost may not have been initialized"
AND the compiler says: "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable packCost may not have been initialized
at carpet.main(carpet.java:50)
"
You need to initialize the variable packCost by doing replacing the current line:
int packCost;
by
int packCost=0;
You are initializing your packCost variable in a conditional statement and the compiler is not smart enough to detect it.
Using else instead of else-if makes it clear to the compiler that one of the conditions will be executed, so packCost is always set:
if (roundPacks <= 4)
packCost = 50;
else
{
packCost = 45;
packCost = packCost + 20;
}
You need to initialise packetCost:
int packCost = 0;
In Java, local variables need to be initialized. If this was a field variable, it would have an implicit default value of 0 (zero).
Here is a link that explains this: http://www.janeg.ca/scjp/lang/defaults.html (disclaimer: not my page)
From this page:
Field variables (class members) are automatically initialized to default values
Local variables (method or constructor variables) are not automatically initialized

Categories