Skip to content

About

A shared memory parallel API for Java with a close syntax to OpenMP

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Simple Parallel API for Java

A shared-memory parallel API for Java with a syntax close to OpenMP. Developed to make it easier for students who only know Java to understand parallel programming concepts before moving on to OpenMP.

Requirements

  • Java 26 or higher
  • Maven (to build and run the unit tests)

Features

  • Parallel regions: parallel(() -> ...), parallel(t -> ...), parallel((t, nt) -> ...)
  • Parallel loops: repeat(iterations, i -> ...), forLess(start, end, i -> ...), forLessOrEqual(start, end, i -> ...)
  • Parallel sections: parallelSections(section(...), ...)
  • Asynchronous tasks: task(() -> ...), taskWithResult(() -> ...), taskwait(futures...)
  • Synchronization directives: master, single, critical (unnamed and named), ordered, barrier
  • Thread management: setMaximumNumberOfThreads(n), getMaximumNumberOfThreads(), resetMaximumNumberOfThreads()

Examples

Parallel Region & Synchronization

ParallelApi parallelApi = new ParallelApi();
parallelApi.setMaximumNumberOfThreads(4);

int[] sum = { 0 };

parallelApi.parallel((t, nt) -> {
    System.out.println("Thread " + t + " of " + nt);

    parallelApi.master(t, () -> System.out.println("Executed only by the master thread (ID 0)"));
    parallelApi.single(() -> System.out.println("Executed once by the first arriving thread"));

    parallelApi.ordered(t, () -> {
        System.out.println("Executed sequentially in thread ID order: " + t);
    });

    parallelApi.barrier();

    parallelApi.critical(() -> {
        sum[0] += (t + 1);
    });

    parallelApi.critical("namedLock", () -> {
        System.out.println("Named critical section for thread " + t);
    });
});

Parallel Loops

int[] squares = new int[10];

// [0, 10)
parallelApi.forLess(0, squares.length, i -> squares[i] = i * i);

// [5, 9] inclusive
parallelApi.forLessOrEqual(5, 9, i -> System.out.println("squares[" + i + "] = " + squares[i]));

// Repeat N times: [0, N)
parallelApi.repeat(8, i -> System.out.print(i));

Parallel Sections

int[] results = new int[3];

parallelApi.parallelSections(
    parallelApi.section(() -> results[0] = 100),
    parallelApi.section(t -> results[1] = 200),
    parallelApi.section((t, nt) -> results[2] = 300)
);

Asynchronous Tasks

Future<Integer> taskA = parallelApi.taskWithResult(() -> 20 + 22);
Future<Void> taskB = parallelApi.task(() -> System.out.println("Background task"));

parallelApi.taskwait(taskA, taskB);
System.out.println("Result: " + taskA.get());

Running Tests

mvn -f um.es.parallel.api/pom.xml test

About

A shared memory parallel API for Java with a close syntax to OpenMP

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages