Running an application on JAVA platform

Z MetaCentrum
Skočit na navigaci Skočit na vyhledávání

(Česká verze)


Metacentrum wiki is deprecated after March 2023
Dear users, due to integration of Metacentrum into https://www.e-infra.cz/en (e-INFRA CZ service), the documentation for users will change format and site.
The current wiki pages won't be updated after end of March 2023. They will, however, be kept for a few months for backwards reference.
The new documentation resides at https://docs.metacentrum.cz.

This page shows how to run an application on JAVA platform. Use the application JDK for running an application on JAVA platform.

Java parallelization

No parallelization

If your application doesn't use threads everything is done in main thread without any parallelization.

You can't usually control threads in an application, which is not yours. You can request the whole machine via the #excl option to solve this problem (see PBS options detailed page).

Thread parallelization

ZarovkaMala.png Note: If you would like to learn more about threads, see The Java Tutorials - Lesson: Concurrency.

You can easily use multiple CPUs on a same machine with JAVA platform because JAVA supports threads.


It is important to realize that one machine is used by multiple users who are supposed not to exceed given resources. JAVA allows to control number of running threads fairly well, make sure you won't run more threads than number of CPUs you requested.

If you requested a whole machine via #excl, you can get number of CPUs in a machine by calling method Runtime.availableProcessors(). Otherwise you have to work with the given number of CPUs if you requested a limited amount of CPUs.

You can call method Executors.newFixedThreadPool(int) to force using n threads at maximum. See Thread Pools for more information.

The following example shows a program using specified number of threads:

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

public class Exekuce {

    static public class Producer implements Callable<String> {

        private final int n;

        public Producer(int n) {
            this.n = n;
        }

        public String call() throws Exception {
            //a difficult computation
            return " Product("+n+")";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //number of threads by parameter if possible, if not by number of CPUs
        int numThreads = Runtime.getRuntime().availableProcessors();
        if(args.length>0) {
            numThreads = Integer.parseInt(args[0]);
        }
        
        //thread pool of specified size
        ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

       
        List<Producer> producers = Arrays.asList(new Producer(1), new Producer(2), new Producer(3));

        //parallel processing
        List<Future<String>> futures = executorService.invokeAll(producers);
        executorService.shutdown();

        //get results
        for (Future<String> fs : futures) {
            System.out.println("výsledek " + fs.get());
        }

    }

Running a JAVA program

1. : Login to any available frontend, except of skirit (there are some memory limitations).

2. : Prepare your program

Prepare following file in you home directory (e.g. /storage/plze1/home/$LOGNAME

HelloWorld.java

    public class HelloWorld {
        public static void main(String args[]) {
            System.out.println("Hello world " + args[0]);
        }
    }

load JDK modul

module add jdk-8

and compile it with javac command:

javac HelloWorld.java -> HelloWorld.class file is created.

3. : Prepare your startup (batch) script

JDK offers whole Java Development Kit (JDK), including Java Runtime Environment (JRE) through the java command.

Prepare following file as your startup script.

task.sh

#!/bin/bash

module add jdk-8 # loads JDK module

java -cp /storage/plzen1/home/$LOGNAME HelloWorld Hello

4. : Submit your job Submit the job with following command:

qsub -q short -l nodes=1:ppn=1:nfs4,mem=1gb -j oe -m e task.sh

You will be noticed when the job is finished via email and you will find results of your program in the directory you submitted the job from (/storage/plzen1/home/$LOGNAME in this example). Results are saved in a file with name similar to task.sh.o12345.