Esempio n. 1
0
  /* Fill the bipartite graph if possible. */
  boolean[] solve() {
    Map<Integer, List<Integer>> gr = makeGraph();

    boolean[] truthTable = new boolean[count];
    // keep track of which ones we've visited
    boolean[] visited = new boolean[count];
    Queue<Integer> queue = new LinkedList<Integer>();
    truthTable[0] = true; // assume the first person tells the truth
    queue.add(0);

    // Breadth first search on graph
    while (!queue.isEmpty()) {
      int next = queue.remove();
      boolean truth = truthTable[next];
      List<Integer> list = gr.get(next);

      // Go through list and toggle when needed
      for (int i = 0; i < list.size(); i++) {
        int node = list.get(i);
        if (!visited[node]) {
          visited[node] = true;
          truthTable[node] = !truth;
          queue.add(node);
        }
      }
    }

    return truthTable;
  }
Esempio n. 2
0
 protected void handleRemoveRunRequest(Owner sender) {
   _consumerLock.lock();
   try {
     _runRequests.remove(sender);
   } finally {
     _consumerLock.unlock();
   }
 }
Esempio n. 3
0
 protected void handleRemoveConsumer(Owner sender) {
   _consumerLock.lock();
   try {
     _consumersAvailable.remove(sender);
   } finally {
     _consumerLock.unlock();
   }
 }
 public synchronized void workAllJobs() {
   while (!jobs.isEmpty()) {
     loadJob(jobs.remove());
   }
   while (!shadertoset.empty()) {
     shadertoset.pop().load();
   }
 }
Esempio n. 5
0
 protected void handleConsumerUnreadyRequest(long requestId, Address address) {
   Owner consumer = new Owner(address, requestId);
   _consumerLock.lock();
   try {
     _consumersAvailable.remove(consumer);
   } finally {
     _consumerLock.unlock();
   }
   sendRemoveConsumerRequest(consumer);
 }
 public void run() {
   latch = new CountDownLatch(queue.size());
   while (!queue.isEmpty()) {
     final LineCounter counter = queue.remove();
     new Thread(
             new Runnable() {
               public void run() {
                 execute(counter);
                 latch.countDown();
               }
             })
         .start();
   }
   waitOnLatch();
 }
Esempio n. 7
0
 public void solve(InputReader in, OutputWriter out) {
   int n = in.nextInt();
   int[][] g = new int[n][n];
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
       g[i][j] = in.nextInt();
     }
   }
   Queue<int[]> queue =
       new PriorityQueue<int[]>(
           1,
           new Comparator<int[]>() {
             @Override
             public int compare(int[] o1, int[] o2) {
               return o1[1] - o2[1];
             }
           });
   queue.add(new int[] {0, 0});
   boolean[] visited = new boolean[n];
   int res = 0;
   while (!queue.isEmpty()) {
     int[] head = queue.remove();
     int node = head[0];
     int dist = head[1];
     if (!visited[node]) {
       res += dist;
       visited[node] = true;
       for (int i = 0; i < n; i++) {
         if (!visited[i]) {
           queue.add(new int[] {i, g[node][i]});
         }
       }
     }
   }
   out.println(res);
 }
Esempio n. 8
0
  public Object down(Event evt) {
    switch (evt.getType()) {
      case ExecutorEvent.TASK_SUBMIT:
        Runnable runnable = evt.getArg();
        // We are limited to a number of concurrent request id's
        // equal to 2^63-1.  This is quite large and if it
        // overflows it will still be positive
        long requestId = Math.abs(counter.getAndIncrement());
        if (requestId == Long.MIN_VALUE) {
          // TODO: need to fix this it isn't safe for concurrent modifications
          counter.set(0);
          requestId = Math.abs(counter.getAndIncrement());
        }

        // Need to make sure to put the requestId in our map before
        // adding the runnable to awaiting consumer in case if
        // coordinator sent a consumer found and their original task
        // is no longer around
        // see https://issues.jboss.org/browse/JGRP-1744
        _requestId.put(runnable, requestId);

        _awaitingConsumer.add(runnable);

        sendToCoordinator(RUN_REQUEST, requestId, local_addr);
        break;
      case ExecutorEvent.CONSUMER_READY:
        Thread currentThread = Thread.currentThread();
        long threadId = currentThread.getId();
        _consumerId.put(threadId, PRESENT);
        try {
          for (; ; ) {
            CyclicBarrier barrier = new CyclicBarrier(2);
            _taskBarriers.put(threadId, barrier);

            // We only send to the coordinator that we are ready after
            // making the barrier, wait for request to come and let
            // us free
            sendToCoordinator(Type.CONSUMER_READY, threadId, local_addr);

            try {
              barrier.await();
              break;
            } catch (BrokenBarrierException e) {
              if (log.isDebugEnabled())
                log.debug(
                    "Producer timed out before we picked up"
                        + " the task, have to tell coordinator"
                        + " we are still good.");
            }
          }
          // This should always be non nullable since the latch
          // was freed
          runnable = _tasks.remove(threadId);
          _runnableThreads.put(runnable, currentThread);
          return runnable;
        } catch (InterruptedException e) {
          if (log.isDebugEnabled()) log.debug("Consumer " + threadId + " stopped via interrupt");
          sendToCoordinator(Type.CONSUMER_UNREADY, threadId, local_addr);
          Thread.currentThread().interrupt();
        } finally {
          // Make sure the barriers are cleaned up as well
          _taskBarriers.remove(threadId);
          _consumerId.remove(threadId);
        }
        break;
      case ExecutorEvent.TASK_COMPLETE:
        Object arg = evt.getArg();
        Throwable throwable = null;
        if (arg instanceof Object[]) {
          Object[] array = (Object[]) arg;
          runnable = (Runnable) array[0];
          throwable = (Throwable) array[1];
        } else {
          runnable = (Runnable) arg;
        }
        Owner owner = _running.remove(runnable);
        // This won't remove anything if owner doesn't come back
        _runnableThreads.remove(runnable);

        Object value = null;
        boolean exception = false;
        if (throwable != null) {
          // InterruptedException is special telling us that
          // we interrupted the thread while waiting but still got
          // a task therefore we have to reject it.
          if (throwable instanceof InterruptedException) {
            if (log.isDebugEnabled())
              log.debug("Run rejected due to interrupted exception returned");
            sendRequest(owner.address, Type.RUN_REJECTED, owner.requestId, null);
            break;
          }
          value = throwable;
          exception = true;
        } else if (runnable instanceof RunnableFuture<?>) {
          RunnableFuture<?> future = (RunnableFuture<?>) runnable;

          boolean interrupted = false;
          boolean gotValue = false;

          // We have the value, before we interrupt at least get it!
          while (!gotValue) {
            try {
              value = future.get();
              gotValue = true;
            } catch (InterruptedException e) {
              interrupted = true;
            } catch (ExecutionException e) {
              value = e.getCause();
              exception = true;
              gotValue = true;
            }
          }

          if (interrupted) {
            Thread.currentThread().interrupt();
          }
        }

        if (owner != null) {
          final Type type;
          final Object valueToSend;
          if (value == null) {
            type = Type.RESULT_SUCCESS;
            valueToSend = value;
          }
          // Both serializable values and exceptions would go in here
          else if (value instanceof Serializable
              || value instanceof Externalizable
              || value instanceof Streamable) {
            type = exception ? Type.RESULT_EXCEPTION : Type.RESULT_SUCCESS;
            valueToSend = value;
          }
          // This would happen if the value wasn't serializable,
          // so we have to send back to the client that the class
          // wasn't serializable
          else {
            type = Type.RESULT_EXCEPTION;
            valueToSend = new NotSerializableException(value.getClass().getName());
          }

          if (local_addr.equals(owner.getAddress())) {
            if (log.isTraceEnabled())
              log.trace(
                  "[redirect] <--> ["
                      + local_addr
                      + "] "
                      + type.name()
                      + " ["
                      + value
                      + (owner.requestId != -1 ? " request id: " + owner.requestId : "")
                      + "]");
            if (type == Type.RESULT_SUCCESS) {
              handleValueResponse(local_addr, owner.requestId, valueToSend);
            } else if (type == Type.RESULT_EXCEPTION) {
              handleExceptionResponse(local_addr, owner.requestId, (Throwable) valueToSend);
            }
          } else {
            sendRequest(owner.getAddress(), type, owner.requestId, valueToSend);
          }
        } else {
          if (log.isTraceEnabled()) {
            log.trace("Could not return result - most likely because it was interrupted");
          }
        }
        break;
      case ExecutorEvent.TASK_CANCEL:
        Object[] array = evt.getArg();
        runnable = (Runnable) array[0];

        if (_awaitingConsumer.remove(runnable)) {
          _requestId.remove(runnable);
          ExecutorNotification notification = notifiers.remove(runnable);
          if (notification != null) {
            notification.interrupted(runnable);
          }
          if (log.isTraceEnabled())
            log.trace("Cancelled task " + runnable + " before it was picked up");
          return Boolean.TRUE;
        }
        // This is guaranteed to not be null so don't take cost of auto unboxing
        else if (array[1] == Boolean.TRUE) {
          owner = removeKeyForValue(_awaitingReturn, runnable);
          if (owner != null) {
            Long requestIdValue = _requestId.remove(runnable);
            // We only cancel if the requestId is still available
            // this means the result hasn't been returned yet and
            // we still have a chance to interrupt
            if (requestIdValue != null) {
              if (requestIdValue != owner.getRequestId()) {
                log.warn("Cancelling requestId didn't match waiting");
              }
              sendRequest(owner.getAddress(), Type.INTERRUPT_RUN, owner.getRequestId(), null);
            }
          } else {
            if (log.isTraceEnabled()) log.warn("Couldn't interrupt server task: " + runnable);
          }
          ExecutorNotification notification = notifiers.remove(runnable);
          if (notification != null) {
            notification.interrupted(runnable);
          }
          return Boolean.TRUE;
        } else {
          return Boolean.FALSE;
        }
      case ExecutorEvent.ALL_TASK_CANCEL:
        array = evt.getArg();

        // This is a RunnableFuture<?> so this cast is okay
        @SuppressWarnings("unchecked")
        Set<Runnable> runnables = (Set<Runnable>) array[0];
        Boolean booleanValue = (Boolean) array[1];

        List<Runnable> notRan = new ArrayList<>();

        for (Runnable cancelRunnable : runnables) {
          // Removed from the consumer
          if (!_awaitingConsumer.remove(cancelRunnable) && booleanValue == Boolean.TRUE) {
            synchronized (_awaitingReturn) {
              owner = removeKeyForValue(_awaitingReturn, cancelRunnable);
              if (owner != null) {
                Long requestIdValue = _requestId.remove(cancelRunnable);
                if (requestIdValue != owner.getRequestId()) {
                  log.warn("Cancelling requestId didn't match waiting");
                }
                sendRequest(owner.getAddress(), Type.INTERRUPT_RUN, owner.getRequestId(), null);
              }
              ExecutorNotification notification = notifiers.remove(cancelRunnable);
              if (notification != null) {
                log.trace("Notifying listener");
                notification.interrupted(cancelRunnable);
              }
            }
          } else {
            _requestId.remove(cancelRunnable);
            notRan.add(cancelRunnable);
          }
        }
        return notRan;
      case Event.SET_LOCAL_ADDRESS:
        local_addr = evt.getArg();
        break;

      case Event.VIEW_CHANGE:
        handleView(evt.getArg());
        break;
    }
    return down_prot.down(evt);
  }
Esempio n. 9
0
  public static void main(String[] args) throws Exception {

    if (args.length != 2) {
      System.err.println("USAGE: java -jar SAMSort.jar <BWA PAIRED SAM file> <configFile>");
    } else {
      System.err.println("Running: java -jar SAMSort.jar " + args[0] + " " + args[1]);
      // args[1]: conf file
      Constants.loadConstants(args[1], false);

      StringBuffer headers = new StringBuffer();

      ArrayList<SAM> list = new ArrayList<SAM>();

      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      int i = 0;
      int count = 0;
      String curline = null;
      String curline2 = null;

      SAM s1 = null;
      SAM s2 = null;

      boolean done = false;

      // int cordantCount = 0;
      // int discordantCount = 0;

      int totalPairCount = 0;
      int discordantPairCount = 0;
      int singleEndMappedPairCount = 0;
      int unmappedPairCount = 0;

      BufferedWriter unmapped_bw = null;
      BufferedWriter se_bw = null;

      while (!done) {
        curline = br.readLine();
        if (curline == null) break;
        else if (!curline.startsWith("@")) {
          s1 = new SAM(curline);
          // skip secondary. Load until primary
          while (s1.isSecondary()) {
            curline = br.readLine();
            if (curline == null) {
              done = true;
              break;
            }
            s1 = new SAM(curline);
          }

          if (done) break;

          curline2 = br.readLine();
          s2 = new SAM(curline2);
          // skip secondary. Load until primary
          while (s2.isSecondary()) {
            curline = br.readLine();
            if (curline == null) {
              done = true;
              break;
            }
            s2 = new SAM(curline);
          }

          if (done) break;

          totalPairCount++;

          // only collect discordant
          SAMPair tmppair = new SAMPair(s1, s2);
          if (tmppair.isDiscordant()) {
            discordantPairCount++;
            list.add(s1);
            list.add(s2);
            s1 = null;
            s2 = null;
            if (list.size() == 200000) {
              Collections.sort(list);
              BufferedWriter bw = new BufferedWriter(new FileWriter(args[0] + ".part_" + i));
              for (SAM s : list) {
                bw.write(s.getSamline() + "\n");
              }

              bw.close();
              bw = null;
              i++;
              list = new ArrayList<SAM>();
            }
          } else if (!tmppair.isBothUnmapped()) { // single-end mapped
            if (se_bw == null) {
              se_bw = new BufferedWriter(new FileWriter(args[0] + ".singleEndMapped"));
              se_bw.write(headers.toString());
            }
            singleEndMappedPairCount++;
            se_bw.write(s1.getSamline() + "\n");
            se_bw.write(s2.getSamline() + "\n");
            s1 = null;
            s2 = null;
          } else { // both unmapped
            if (unmapped_bw == null) {
              unmapped_bw = new BufferedWriter(new FileWriter(args[0] + ".unmapped"));
              unmapped_bw.write(headers.toString());
            }
            unmappedPairCount++;
            unmapped_bw.write(s1.getSamline() + "\n");
            unmapped_bw.write(s2.getSamline() + "\n");
            s1 = null;
            s2 = null;
          }

        } else {
          headers.append(curline + "\n");
        }
      }
      br.close();
      br = null;

      if (se_bw != null) se_bw.close();
      if (unmapped_bw != null) unmapped_bw.close();

      if (list.size() > 0) {
        Collections.sort(list);
        BufferedWriter bw = new BufferedWriter(new FileWriter(args[0] + ".part_" + i));
        // System.err.println(count + "\t" + set.size());
        for (SAM s : list) {
          bw.write(s.getSamline() + "\n");
        }
        bw.close();
        bw = null;
        i++;
        list = null;
      }

      Map<SAM, Queue<SAM>> map = new TreeMap<SAM, Queue<SAM>>();
      // Set<SAM> set = new TreeSet<SAM>();
      // System.err.println("openning " + i + " files");
      BufferedReader[] brArr = new BufferedReader[i];
      for (int j = 0; j < i; j++) {
        brArr[j] = new BufferedReader(new FileReader(args[0] + ".part_" + j));
        String tmpline = brArr[j].readLine();
        SAM tmp = new SAM(tmpline, j);
        if (!map.containsKey(tmp)) map.put(tmp, new LinkedList<SAM>());
        else {
          // System.err.println("here");
          map.get(tmp).add(tmp); // brArr[j].pushBack(tmpline);
        }
        // System.err.println(map.get(tmp).size());
      }
      // System.err.println(map.size());

      BufferedWriter bw = new BufferedWriter(new FileWriter(args[0] + ".discordant.midsorted"));
      bw.write(headers.toString());
      SAM s = null;

      // int counter = 0;
      Queue<SAM> q = null;
      while (!map.isEmpty()) {
        s = map.keySet().iterator().next();
        q = map.get(s);
        // map.remove(s);
        bw.write(s.getSamline());
        bw.write("\n");
        if (q.size() == 0) {
          map.remove(s);
          int tmpIndex = s.getIndex();
          String tmpline = brArr[tmpIndex].readLine();
          if (tmpline != null) {
            s = new SAM(tmpline, tmpIndex);
            if (!map.containsKey(s)) {
              // System.err.print("put b4: " + map.size());
              map.put(s, new LinkedList<SAM>());
              // System.err.println("\ta4: " + map.size());
            } else {
              // System.err.print("here2: ");
              map.get(s).add(s);
              // System.err.println(map.size());
            }
          } // else{
          // System.err.println("reader[" + tmpIndex + "] : NULL");
          // }
        } else {
          // System.err.print("B4 Q size: " + q.size() + " map size: " + map.size() );
          map.remove(s);
          map.put(q.remove(), q);
          // System.err.println("\tA4 Q size: " + q.size() + " map size: " + map.size() );
          int tmpIndex = s.getIndex();
          String tmpline = brArr[tmpIndex].readLine();
          if (tmpline != null) {
            s = new SAM(tmpline, tmpIndex);
            if (!map.containsKey(s)) {
              // System.err.print("put b4: " + map.size());
              map.put(s, new LinkedList<SAM>());
              // System.err.println("\ta4: " + map.size());
            } else {
              // System.err.print("here2: ");
              map.get(s).add(s);
              // System.err.println(map.size());
            }
          } // else{
          //  System.err.println("reader[" + tmpIndex + "] : NULL");
          // }

        }
        // counter++;

      }
      // System.err.println(counter);
      bw.close();

      for (int j = 0; j < brArr.length; j++) {
        brArr[j].close();
        new File(args[0] + ".part_" + j).delete();
      }

      //	int totalPairCount = 0;
      // int discordantPairCount = 0;
      System.out.println("------ DONE WITH REMOVAL OF CONCORDANT & UNMAPPED PAIRS ---------");
      System.out.println("------ Processed a total of\t" + totalPairCount + " read-pairs");
      System.out.println(
          "------ Retained a total of\t"
              + discordantPairCount
              + " read-pairs writtend out to "
              + args[0]
              + ".discordant.midsorted");
      System.out.println(
          "------ Single-end mapped :\t"
              + singleEndMappedPairCount
              + " read-pairs written out to "
              + args[0]
              + ".singleEndMapped");
      System.out.println(
          "------ Unmapped (both ends) : \t"
              + unmappedPairCount
              + " read-pairs written out to "
              + args[0]
              + ".unmapped");
    }
  }
Esempio n. 10
0
 /*Method that gives new piece for a thread to get*/
 public synchronized int nextNeededPiece() {
   synchronized (neededPieces) {
     if (neededPieces.peek() == null) return -1;
     return neededPieces.remove();
   }
 }
 public synchronized void reloadRessources() {
   while (!oldjobs.isEmpty()) {
     oldjobs.remove().load();
   }
 }
 public synchronized void workJob() {
   if (!jobs.isEmpty()) {
     loadJob(jobs.remove());
   }
 }
Esempio n. 13
0
 public void STS() // method to execute an instruction when the current process finishes
     {
   while (!queue.isEmpty() && (STATE_COMPLETED)) {
     CPU.execute(queue.remove());
   }
 }