AparapiRunner.java

/*
 * JPPF.
 * Copyright (C) 2005-2019 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jppf.example.aparapi;

import java.util.List;

import org.jppf.client.*;
import org.jppf.node.protocol.Task;
import org.jppf.utils.*;
import org.slf4j.*;

/**
 * 
 * @author Laurent Cohen
 */
public class AparapiRunner {
  /**
   * Logger for this class.
   */
  private static Logger log = LoggerFactory.getLogger(AparapiRunner.class);
  /**
   * The JPPF client singleton.
   */
  private static JPPFClient client = null;

  /**
   * Entry poit for this applications.
   * @param args command line arguments are not used.
   * @throws Throwable if any error occurs.
   */
  public static void main(final String[] args) throws Throwable {
    try {
      print("creating client");
      client = new JPPFClient();
      perform();
    } catch (final Exception e) {
      e.printStackTrace();
    } finally {
      client.close();
    }
  }

  /**
   * Submit matrix multiplication jobs to the grid, for execution on a GPU on the nodes' machines.
   * @throws Throwable if any error occurs.
   */
  public static void perform() throws Throwable {
    final TypedProperties config = JPPFConfiguration.getProperties();
    final int iterations = config.getInt("iterations", 10);
    final int tasksPerJob = config.getInt("tasksPerJob", 1);
    final int matrixSize = config.getInt("matrixSize", 1500);
    String execMode = config.getString("execMode", "GPU");
    if (!"GPU".equalsIgnoreCase(execMode) && !"JTP".equalsIgnoreCase(execMode)) execMode = "GPU";
    print("starting GPU test with " + iterations + " jobs, " + tasksPerJob + " tasks per job and a matrix size of " + matrixSize + ", execution mode: " + execMode);

    // initial values for execution timing stats
    long totalIterationTime = 0L;
    long min = Long.MAX_VALUE;
    long max = 0L;

    // one job per iteration
    for (int n = 0; n < iterations; n++) {
      final SquareMatrix matrixA = new SquareMatrix(matrixSize);
      matrixA.assignRandomValues();
      final SquareMatrix matrixB = new SquareMatrix(matrixSize);
      matrixB.assignRandomValues();
      final long start = System.nanoTime();
      final JPPFJob job = new JPPFJob();
      job.setName("gpu_job_" + n);
      for (int i = 0; i < tasksPerJob; i++)
        job.add(new AparapiTask(matrixA, matrixB, execMode));
      // submit and get the results
      final List<Task<?>> results = client.submit(job);
      for (final Task<?> task : results) {
        if (task.getThrowable() != null) throw task.getThrowable();
        final AparapiTask t = (AparapiTask) task;
        assert t.getResult() instanceof SquareMatrix;
        //print("result for " + task.getId() + ": " + task.getResult());
      }
      final long elapsed = (System.nanoTime() - start) / 1000000;
      if (elapsed < min) min = elapsed;
      if (elapsed > max) max = elapsed;
      totalIterationTime += elapsed;
      print("Iteration #" + (n + 1) + " performed in " + StringUtils.toStringDuration(elapsed));
    }
    print("total time: " + StringUtils.toStringDuration(totalIterationTime) + ", average time: " + StringUtils.toStringDuration(totalIterationTime / iterations) + ", min = "
        + StringUtils.toStringDuration(min) + ", max = " + StringUtils.toStringDuration(max));
  }

  /**
   * Print a message to the log and to the console.
   * @param msg the message to print.
   */
  private static void print(final String msg) {
    System.out.println(msg);
    log.info(msg);
  }
}