I have been using GCP compute engine to run a permutation calculator, it is designed to run concurrently and so should yield 1500%cpu usage on a 16 core high cpu instance; however it is in fact 90%. there is no synchronization and each thread runs completely independantly of one another (they can even be run on different processes without effecting the end result).
App.java:
package com.syd;
public class App {
public static void main(String[] args) {
int n;
int threads;
try{
n = Integer.parseInt(args[0]);
threads = Integer.parseInt(args[1]);
}catch(Exception e){
System.out.println("Default App Values!");
n=5;
threads =4;
}
System.out.printf("====%n %d CARDS%n====%n %d THREADS%n====%n", n, threads);
Chain[] chain = new Chain[threads];
for(int i=0;i<threads;i++){
chain[i] = new Chain(n,i,threads);
chain[i].start();
}
for(int i=0;i<threads;i++){
boolean complete = false;
while(!complete){
try{
chain[i].getThread().join();
complete = true;
}catch(Exception e){
e.printStackTrace();
}
}
}
new Add(n,threads).run();
}
}
Chain.java
package com.syd;
import java.io.*;
import java.io.File;
import java.math.BigInteger;
public class Chain implements Runnable{
private int n;
private int thread;
private int threads;
private Thread t;
public static void main(String[] args) {
try{
new Chain(Integer.parseInt(args[0]), Integer.parseInt(args[1]),Integer.parseInt(args[2])).run();
}catch(Exception e){
System.out.println("Default Chain Values!");
new Chain(5,0,4).run();
}
}
public Chain(int n, int thread, int threads){
this.n=n;
this.thread=thread;
this.threads=threads;
System.out.printf("Chain(%d,%d,%d)%n",n, thread, threads);
}
public void start(){
if(t==null){
t = new Thread(this);
t.start();
}
}
public Thread getThread(){
return t;
}
public void run(){
new File("./res/").mkdir();
String path = String.format("./res/%dn%dt%d.txt",n,thread,threads);
try{
PrintWriter writer = new PrintWriter(path, "UTF-8");
for(int i=0;i<10;i++){
writer.println("0");
}
writer.flush();
writer.close();
}catch(Exception e){
e.printStackTrace();
}
BigInteger maxPerms = new BigInteger("1");
for(int i=52;i>52-n;i--){
maxPerms = maxPerms.multiply(new BigInteger(String.format("%d",i)));
}
//System.out.printf("maxPerms:%s%n",maxPerms);
BigInteger max = (maxPerms.multiply(new BigInteger(String.format("%d", thread+1)))).divide(new BigInteger(new Integer(threads).toString()));
BigInteger current = (maxPerms.multiply(new BigInteger(String.format("%d", thread)))).divide(new BigInteger(new Integer(threads).toString()));
do{
BigInteger from = current;
current = current.add(new BigInteger("5000000"));
if (current.subtract(max).doubleValue()>0){
current = max;
}
System.out.print(".");
new Worker(n, from, current, path).run();
}while(current.subtract(max).doubleValue()<0);
}
}
running
sudo nohup java -cp . com.syd.App 5 4
yields 90%cpu
running
sudo nohup java -cp . com.syd.Chain 5 0 4
(90% cpu for that process at this point) followed by
sudo nohup java -cp . com.syd.Chain 5 1 4
(in another terminal window) yields 45%cpu each process
im thinking its probably some config for the google cloud platform to enable the other cores as the command top only shows one cpu.
any help is appreciated, thanks