Initial check-in of Acxxel (StreamExecutor renamed)
Summary:
Acxxel is basically a simplified redesign of StreamExecutor.
Here are the major points where Acxxel differs from the current
StreamExecutor design:
* Acxxel doesn't support the kernel and kernel loader types designed for
emission by the compiler to support type-safe kernel launches. For
CUDA, kernels in Acxxel can be seamlessly launched using the standard
CUDA triple-chevron kernel launch syntax that is available with clang
and nvcc. For CUDA and OpenCL, kernel arguments can be passed in the
old-fashioned way, as one array of pointers to arguments and another
array of argument sizes. Although OpenCL doesn't get a type-safe
kernel launch method, it does still get the benefit of all the memory
management wrappers. In the future, clang may add support for
triple-chevron OpenCL kernel launchs, or some other type-safe OpenCL
kernel launch method.
* Acxxel does not depend on any other code in LLVM, so it builds
completely independently from LLVM.
The goal will be to check in Acxxel and remove StreamExecutor, or
perhaps to remove the old StreamExecutor and rename Acxxel to
StreamExecutor, so I think Acxxel should be thought of as a new version
of StreamExecutor, not as a separate project.
Reviewers: jlebar, jprice
Subscribers: beanz, mgorny, modocache, parallel_libs-commits
Differential Revision: https://reviews.llvm.org/D25701
llvm-svn: 285111
2016-10-26 04:18:56 +08:00
|
|
|
//===--- opencl_example.cpp - Example of using Acxxel with OpenCL ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// This file is an example of using OpenCL with Acxxel.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "acxxel.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
static const char *SaxpyKernelSource = R"(
|
|
|
|
__kernel void saxpyKernel(float A, __global float *X, __global float *Y, int N) {
|
|
|
|
int I = get_global_id(0);
|
|
|
|
if (I < N)
|
|
|
|
X[I] = A * X[I] + Y[I];
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void saxpy(float A, std::array<float, N> &X, const std::array<float, N> &Y) {
|
|
|
|
acxxel::Platform *OpenCL = acxxel::getOpenCLPlatform().getValue();
|
|
|
|
acxxel::Stream Stream = OpenCL->createStream().takeValue();
|
|
|
|
auto DeviceX = OpenCL->mallocD<float>(N).takeValue();
|
|
|
|
auto DeviceY = OpenCL->mallocD<float>(N).takeValue();
|
|
|
|
Stream.syncCopyHToD(X, DeviceX).syncCopyHToD(Y, DeviceY);
|
|
|
|
acxxel::Program Program =
|
|
|
|
OpenCL
|
|
|
|
->createProgramFromSource(acxxel::Span<const char>(
|
|
|
|
SaxpyKernelSource, std::strlen(SaxpyKernelSource)))
|
|
|
|
.takeValue();
|
|
|
|
acxxel::Kernel Kernel = Program.createKernel("saxpyKernel").takeValue();
|
|
|
|
float *RawX = static_cast<float *>(DeviceX);
|
|
|
|
float *RawY = static_cast<float *>(DeviceY);
|
|
|
|
int IntLength = N;
|
|
|
|
void *Arguments[] = {&A, &RawX, &RawY, &IntLength};
|
|
|
|
size_t ArgumentSizes[] = {sizeof(float), sizeof(float *), sizeof(float *),
|
|
|
|
sizeof(int)};
|
|
|
|
acxxel::Status Status =
|
|
|
|
Stream.asyncKernelLaunch(Kernel, N, Arguments, ArgumentSizes)
|
|
|
|
.syncCopyDToH(DeviceX, X)
|
|
|
|
.sync();
|
|
|
|
if (Status.isError()) {
|
|
|
|
std::fprintf(stderr, "Error during saxpy: %s\n",
|
|
|
|
Status.getMessage().c_str());
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
float A = 2.f;
|
2016-12-20 05:34:07 +08:00
|
|
|
std::array<float, 3> X{{0.f, 1.f, 2.f}};
|
|
|
|
std::array<float, 3> Y{{3.f, 4.f, 5.f}};
|
|
|
|
std::array<float, 3> Expected{{3.f, 6.f, 9.f}};
|
Initial check-in of Acxxel (StreamExecutor renamed)
Summary:
Acxxel is basically a simplified redesign of StreamExecutor.
Here are the major points where Acxxel differs from the current
StreamExecutor design:
* Acxxel doesn't support the kernel and kernel loader types designed for
emission by the compiler to support type-safe kernel launches. For
CUDA, kernels in Acxxel can be seamlessly launched using the standard
CUDA triple-chevron kernel launch syntax that is available with clang
and nvcc. For CUDA and OpenCL, kernel arguments can be passed in the
old-fashioned way, as one array of pointers to arguments and another
array of argument sizes. Although OpenCL doesn't get a type-safe
kernel launch method, it does still get the benefit of all the memory
management wrappers. In the future, clang may add support for
triple-chevron OpenCL kernel launchs, or some other type-safe OpenCL
kernel launch method.
* Acxxel does not depend on any other code in LLVM, so it builds
completely independently from LLVM.
The goal will be to check in Acxxel and remove StreamExecutor, or
perhaps to remove the old StreamExecutor and rename Acxxel to
StreamExecutor, so I think Acxxel should be thought of as a new version
of StreamExecutor, not as a separate project.
Reviewers: jlebar, jprice
Subscribers: beanz, mgorny, modocache, parallel_libs-commits
Differential Revision: https://reviews.llvm.org/D25701
llvm-svn: 285111
2016-10-26 04:18:56 +08:00
|
|
|
saxpy(A, X, Y);
|
|
|
|
for (int I = 0; I < 3; ++I)
|
|
|
|
if (X[I] != Expected[I]) {
|
|
|
|
std::fprintf(stderr, "Mismatch at position %d, %f != %f\n", I, X[I],
|
|
|
|
Expected[I]);
|
|
|
|
std::exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|