5 Replies Latest reply: Feb 3, 2007 4:48 PM by 807606 RSS

    java threading problem... threads only work on 1 processor

    807606
      I've got a iterative deepening problem that i have to parallelize. As far as i know i did everything correctly however everything seems to be running on 1 processor instead of 2 processors. As far as i know i am using threads (-Xprof either defines them as thread-1 thread-2 or pool1-thread1,depending on the method used for issueing)

      the worker thread is:
      public int solutionsT(Board board, int currentDepth) {
              int temp = 0;
              int result = 0;
              if (board.distance() == 0) {
                 return 1;
              }
              if (board.distance() > board.bound()) {
                  return 0;
              }
              Board[] children = board.makeMoves();
              result = 0;
              
              for (int i = 0; i < children.length; i++) {
                  if (children[i] != null) {
                      temp = solutionsT(children, currentDepth + 1);
      if(temp != 0){
      result += temp;
      }
      }
      }
      return result;
      }

      public void run() {
      int temp =0;
      int i = 0;

      while(true){

      while(bag.size() !=0){
      bag.putSolution(solutionsT(bag.get(),1));
      }
      try{   
      barrier.await();
      }catch(Exception e){}
      }
      }
      }

      it get's it's input from a bag that is filled before the iteration begins. (once the bag is filled it trips a barrier) this worker thread is a implementation of Runnable

      This piece of code is used to make the thread object and to issue it
      public SolutionThread(int numberOfThreads) {
         thread = numberOfThreads;
         bag = new bagOfBoards();
         barrier = new CyclicBarrier(thread+1);
         if(thread > 1){
            ExecutorService threadExecutor = Executors.newFixedThreadPool( thread );
            solution = new ThreadTest[thread];
            bag = new bagOfBoards();
            for(int i = 0;i<thread;i++){
               solution[i] = new ThreadTest(bag, lock, barrier, i);
               threadExecutor.execute(solution); 
      }
      }
      }

      finally this is the code which is used to acces the bag and get a board.
      synchronized public Board get() {
                  if (size > 0) {
                  size--;
                  Board result = bag[size];
                  return result;
              } else {
               return null;
               }
          }
      since this method is synchronized and it always returns something (either null or a board) and the worker tests for this. there is no race condition here.

      furter more. the main thread is a loop. It fills te bags with an intial state. then it trips the barrier and waits for the workers to do the work. the workers then process the bag until it hits zero and then trip the barrier so that the main thread can do the next iteration (and fill the bag)

      p.s. i know the code is a bit messy, but i want to get the threading to work. As of now i relaly don't understand why the threads are just running on 1 processor instead of 2 processors. not only that. the excecution time is nearly the same as that of a sequential equivalent.

      p.s.2 the code is parallisable. and it is run on a smp system.

      Message was edited by:
      jstrike