Skip to content

Commit

Permalink
Merge pull request #608 from jjfumero/feat/spirv/decoupled
Browse files Browse the repository at this point in the history
[feat] Individual selection of Level Zero and/or OpenCL runtimes for SPIR-V
  • Loading branch information
jjfumero authored Jan 8, 2025
2 parents 4a4a00f + 7b92f6c commit 7faed3e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
4 changes: 2 additions & 2 deletions docs/source/spirv-backend.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SPIR-V Devices
\SPIR-V Devices
====================================

SPIR-V makes use of the `Intel Level Zero API <https://spec.oneapi.io/level-zero/latest/index.html>`__.
Expand Down Expand Up @@ -69,7 +69,7 @@ TornadoVM/Java Options for SPIR-V:

- ``-Dtornado.spirv.version=1.2``: Modify the minimum version supported. By default is 1.2. However, developers can change this value. Note that the generated code might not work, as TornadoVM requires at least 1.2.

- ``-Dtornado.spirv.dispatcher=opencl``: It sets the runtime to dispatch SPIR-V kernels. Allowed values are: ``opencl`` and ``levelzero``.
- ``-Dtornado.spirv.runtimes=opencl,levelzero``: It sets the list of supported runtimes to dispatch SPIR-V. Allowed values are: ``opencl`` and ``levelzero``. They are separated by a comma, and the first in the list is taken as default.

- ``-Dtornado.spirv.levelzero.extended.memory=True``: It uses Level Zero extended memory mode. It is set to ``true`` by default.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
@Opcode("Unroll")
public class OCLPragmaUnroll extends OCLLIROp {

private int unroll;
private final int unrollFactor;

public OCLPragmaUnroll(int unroll) {
public OCLPragmaUnroll(int unrollFactor) {
super(LIRKind.Illegal);
this.unroll = unroll;
this.unrollFactor = unrollFactor;
}

@Override
public void emit(OCLCompilationResultBuilder crb, OCLAssembler asm) {
asm.emitLine("#pragma unroll " + unroll);
asm.emitLine("#pragma unroll " + unrollFactor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public SPIRVBackendImpl(OptionValues options, HotSpotJVMCIRuntime vmRuntime, Tor
logger.info("[SPIR-V] Found %d platforms", numPlatforms);

if (numPlatforms < 1) {
throw new TornadoBailoutRuntimeException("[Warning] No SPIR-V platforms found. Deoptimizing to sequential execution");
throw new TornadoBailoutRuntimeException("[ERROR] No SPIR-V platforms found. Deoptimizing to sequential execution");
}
platforms = new ArrayList<>();
spirvBackends = new SPIRVBackend[numPlatforms][];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

public class SPIRVOpenCLPlatform implements SPIRVPlatform {

private TornadoPlatformInterface oclPlatform;
private final TornadoPlatformInterface oclPlatform;
private OCLContextInterface context;
private List<SPIRVDevice> spirvDevices;
private final List<SPIRVDevice> spirvDevices;

public SPIRVOpenCLPlatform(int platformIndex, TornadoPlatformInterface oclPlatform) {
this.oclPlatform = oclPlatform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of Tornado: A heterogeneous programming framework:
* https://github.com/beehive-lab/tornadovm
*
* Copyright (c) 2021, 2024, APT Group, Department of Computer Science,
* Copyright (c) 2021, 2024-2025, APT Group, Department of Computer Science,
* School of Engineering, The University of Manchester. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand All @@ -24,7 +24,9 @@
package uk.ac.manchester.tornado.drivers.spirv;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import uk.ac.manchester.tornado.api.exceptions.TornadoRuntimeException;
Expand All @@ -43,8 +45,6 @@
*/
public class SPIRVRuntimeImpl {

private final String ERROR_PLATFORM_NOT_IMPLEMENTED = "SPIR-V Runtime Implementation not supported: " + TornadoOptions.SPIRV_DISPATCHER + " \nUse \"opencl\" or \"levelzero\"";

private List<SPIRVPlatform> platforms;
private static SPIRVRuntimeImpl instance;

Expand All @@ -59,21 +59,24 @@ private SPIRVRuntimeImpl() {
init();
}

private String printErrorPlatformNotImplemented(String spirvPlatform) {
return "SPIR-V Runtime Implementation not supported: " + spirvPlatform + " \nUse \"opencl\" or \"levelzero\"";
}

private SPIRVDispatcher instantiateDispatcher(String runtimeName) {
if (runtimeName.equalsIgnoreCase("opencl")) {
return new SPIRVOpenCLDriver();
} else if (runtimeName.equalsIgnoreCase("levelzero")) {
return new SPIRVLevelZeroDriver();
} else {
throw new TornadoRuntimeException(printErrorPlatformNotImplemented(runtimeName));
}
}

private synchronized void init() {
if (platforms == null) {
SPIRVDispatcher[] dispatchers = new SPIRVDispatcher[SPIRVRuntimeType.values().length];
SPIRVDispatcher levelZeroDriver = new SPIRVLevelZeroDriver();
SPIRVDispatcher openCLDriver = new SPIRVOpenCLDriver();
int index = 0;
if (TornadoOptions.SPIRV_DISPATCHER.equalsIgnoreCase("opencl")) {
dispatchers[index++] = openCLDriver;
dispatchers[index] = levelZeroDriver;
} else if (TornadoOptions.SPIRV_DISPATCHER.equalsIgnoreCase("levelzero")) {
dispatchers[index++] = levelZeroDriver;
dispatchers[index] = openCLDriver;
} else {
throw new TornadoRuntimeException(ERROR_PLATFORM_NOT_IMPLEMENTED);
}
String[] listOfRuntimes = TornadoOptions.SPIRV_INSTALLED_RUNTIMES.split(",");
List<SPIRVDispatcher> dispatchers = Arrays.stream(listOfRuntimes).map(this::instantiateDispatcher).toList();

platforms = new ArrayList<>();
for (SPIRVDispatcher dispatcher : dispatchers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file is part of Tornado: A heterogeneous programming framework:
* https://github.com/beehive-lab/tornadovm
*
* Copyright (c) 2013-2024, APT Group, Department of Computer Science,
* Copyright (c) 2013-2025, APT Group, Department of Computer Science,
* The University of Manchester. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -217,10 +217,22 @@ public class TornadoOptions {
* It enables inlining during Java bytecode parsing. Default is False.
*/
public static final boolean INLINE_DURING_BYTECODE_PARSING = getBooleanValue("tornado.compiler.bytecodeInlining", FALSE);

/**
* Use Level Zero or OpenCL as the SPIR-V Code Dispatcher and Runtime. Allowed values: "opencl", "levelzero". The default option is "opencl".
* List of installed SPIR-V runtimes. Allowed values : "opencl,levelzero". The first in the list is set to the
* default one.
*
* <p>
* <ul>
* <il>Use <code>-Dtornado.spirv.runtimes=opencl</code> for OpenCL only.
* <il>Use <code>-Dtornado.spirv.runtimes=levelzero</code> for LevelZero only.
* <il>Use <code>-Dtornado.spirv.runtimes=opencl,levelzero</code> for both OpenCL and Level Zero runtimes, being
* OpenCL the first in the list (default).
* *</ul>
* </p>
*/
public static final String SPIRV_DISPATCHER = getProperty("tornado.spirv.dispatcher", "opencl");
public static final String SPIRV_INSTALLED_RUNTIMES = getProperty("tornado.spirv.runtimes", "opencl,levelzero");

/**
* Check I/O parameters for every task within a task-graph.
*/
Expand Down

0 comments on commit 7faed3e

Please sign in to comment.