4

I suspect this is really easy but I’m unsure if there’s a naïve way of doing it in Java. Here’s my problem, I have two scripts for processing data and both have the same inputs/outputs except one is written for the single CPU and the other is for GPUs. The work comes from a queue server and I’m trying to write a program that sends the data to either the CPU or GPU script depending on which one is free.

I do not understand how to do this.

I know with executorservice I can specify how many threads I want to keep running but not sure how to balance between two different ones. I have 2 GPU’s and 8 CPU cores on the system and thought I could have threadexecutorservice keep 2 GPU and 8 CPU processes running but unsure how to balance between them since the GPU will be done a lot quicker than the CPU tasks.

Any suggestions on how to approach this? Should I create two queues and keep pooling them to see which one is less busy? or is there a way to just put all the work units(all the same) into one queue and have the GPU or CPU process take from the same queue as they are free?

UPDATE: just to clarify. the CPU/GPU programs are outside the scope of the program I'm making, they are simply scripts that I call via two different method. I guess the simplified version of what I'm asking is if two methods can take work from the same queue?

2
  • 2
    Not sure how you use the GPU, but can't you simply have 10 threads (in an executorservice or 2 - not sure about the GPU part) running something like while (true) { Task t = yourQueue.take(); t.run();}? So each thread can go and take a new task when it is done, independently of what the other are doing.
    – assylias
    Feb 11, 2013 at 15:39
  • @assylias I'm not sure I fully understand(I may not have explained it correctly either). If I have two methods, how can they get data from the same queue using your sample code? Wouldn't that just send all the work to one method? (sorry I'm a newbie at Java but I don't understand still).
    – Lostsoul
    Feb 11, 2013 at 15:58

4 Answers 4

2

Can two methods take work from the same queue?

Yes, but you should use a BlockingQueue to save yourself some synchronization heartache.

Basically, one option would be to have a producer which places tasks into the queue via BlockingQueue.offer. Then design your CPU/GPU threads to call BlockingQueue.take and perform work on whatever they receive.

For example:

main (...) {
    BlockingQueue<Task> queue = new LinkedBlockingQueue<>();


    for (int i=0;i<CPUs;i++) {
        new CPUThread(queue).start();
    }

    for (int i=0;i<GPUs;i++) {
        new GPUThread(queue).start();
    }

    for (/*all data*/) {
        queue.offer(task);
    }
}
class CPUThread {
    public void run() {
        while(/*some condition*/) {
            Task task = queue.take();
            //do task work
        }
    }
}
//etc...
1

Obviously there is more than one way to do it, usually simplest is the best. I would suggest threadpools, one with 2 threads for CPU tasks, second with 8 threads will run GPU tasks. Your work unit manager can submit work to the pool that has idle threads at the moment (I would recommend synchronizing that block of code). Standard Java ThreadPoolExecutor has getActiveCount() method you can use for it, see http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ThreadPoolExecutor.html#getActiveCount().

1

Use Runnables like this:

CPUGPURunnable implements Runnable {
  run() {
    if ( Thread.currentThread() instance of CPUGPUThread) {
      CPUGPUThread t = Thread.currentThread();
      if ( t.isGPU())
        runGPU();
      else
        runCPU();
    }
  }
}

CPUGPUThreads is a Thread subclass that knows if it runs in CPU or GPU mode, using a flag. Have a ThreadFactory for ThreadPoolExecutors that creates either a CPU of GPU thread. Set up a ThreadPoolExecutor with two workers. Make sure the Threadfactory creates a CPU and then a GPU thread instance.

2
  • …runCPU and runGPU are your two methods to send the jobs to GPU or do it on CPU.
    – Ralf H
    Feb 11, 2013 at 17:10
  • I was assuming you want to execute one CPU job and one GPU job in parallel at any one time. Of course, if you have more than one core and runCPU is single-threaded, use more workers. Same for the GPU side.
    – Ralf H
    Feb 11, 2013 at 17:13
1

I suppose you have two objects that represents two GPUs, with methods like boolean isFree() and void execute(Runnable). Then you should start 8 threads which in a loop take next job from the queue, put it in a free GPU, if any, otherwise execute the job itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.