Commit bc3c58bd authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding Kernel class, and setting up Program to take a file at construction

parent 06e805b5
......@@ -130,8 +130,8 @@ public:
* so return an empty list, for now.
* @return
*/
virtual std::vector<xacc::IRTransformation> getIRTransformations() {
std::vector<xacc::IRTransformation> v;
virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() {
std::vector<std::shared_ptr<IRTransformation>> v;
return v;
}
......
......@@ -54,7 +54,7 @@ public:
return true;
}
virtual std::vector<xacc::IRTransformation> getIRTransformations() {
virtual std::vector<std::shared_ptr<xacc::IRTransformation>> getIRTransformations() {
};
......
......@@ -104,8 +104,8 @@ public:
* so return an empty list, for now.
* @return
*/
virtual std::vector<xacc::IRTransformation> getIRTransformations() {
std::vector<xacc::IRTransformation> v;
virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() {
std::vector<std::shared_ptr<IRTransformation>> v;
return v;
}
......
......@@ -99,8 +99,8 @@ public:
* so return an empty list
* @return
*/
virtual std::vector<xacc::IRTransformation> getIRTransformations() {
std::vector<xacc::IRTransformation> v;
virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() {
std::vector<std::shared_ptr<IRTransformation>> v;
return v;
}
......
......@@ -112,8 +112,8 @@ public:
}
virtual std::vector<IRTransformation> getIRTransformations() {
std::vector<IRTransformation> ts;
virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() {
std::vector<std::shared_ptr<IRTransformation>> ts;
return ts;
}
......
......@@ -102,7 +102,7 @@ public:
*
* @return transformations The IR transformations this Accelerator exposes
*/
virtual std::vector<IRTransformation> getIRTransformations() = 0;
virtual std::vector<std::shared_ptr<IRTransformation>> getIRTransformations() = 0;
/**
* Execute the provided XACC IR Function on the provided AcceleratorBuffer.
......
#ifndef XACC_PROGRAM_KERNEL_HPP_
#define XACC_PROGRAM_KERNEL_HPP_
#include "Function.hpp"
namespace xacc {
/**
* The Kernel represents an functor to be executed
* on the attached Accelerator.
*/
template<typename... RuntimeArgs>
class Kernel {
protected:
std::shared_ptr<Function> function;
std::shared_ptr<Accelerator> accelerator;
public:
Kernel(std::shared_ptr<Accelerator> acc, std::shared_ptr<Function> f) :
function(f), accelerator(acc) {
}
Kernel(const Kernel& k) : function(k.function), accelerator(k.accelerator) {}
void operator()(std::shared_ptr<AcceleratorBuffer> buffer,
RuntimeArgs ... args) {
if (sizeof...(RuntimeArgs) > 0) {
// Store the runtime parameters in a tuple
auto argsTuple = std::make_tuple(args...);
// Loop through the tuple, and add InstructionParameters
// to the parameters vector.
std::vector<InstructionParameter> parameters;
xacc::tuple_for_each(argsTuple, [&](auto value) {
parameters.push_back(InstructionParameter(value));
});
// Evaluate all Variable Parameters
function->evaluateVariableParameters(parameters);
}
// Execute the Kernel on the Accelerator
accelerator->execute(buffer, function);
}
void prepend(const std::shared_ptr<Function> prependFunction) {
function->insertInstruction(0, prependFunction);
}
};
}
#endif
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
......@@ -45,6 +45,7 @@
#include "RuntimeOptions.hpp"
#include "Preprocessor.hpp"
#include "ServiceRegistry.hpp"
#include "Kernel.hpp"
namespace xacc {
......@@ -106,11 +107,34 @@ protected:
*/
std::shared_ptr<Compiler> compiler;
public:
/**
* The Constructor, takes the Accelerator
* to execute on, and the source to compile and execute
*
* @param acc Attached Accelerator to execute
* @param sourceFile The kernel source code
*/
Program(std::shared_ptr<Accelerator> acc, const std::string& sourceFile) :
src(sourceFile), accelerator(std::move(acc)) {
}
/**
* The Constructor, takes the Accelerator to execute on,
* and the source kernel string as a stream.
* @param acc Attached Accelerator to execute
* @param stream The file stream containing kernel source code
*/
Program(std::shared_ptr<Accelerator> acc, std::istream& stream) :
accelerator(std::move(acc)), src(
std::istreambuf_iterator<char>(stream), { }) {
}
/**
* Execute the compilation mechanism on the provided program
* source kernel code to produce XACC IR that can be executed
* on the attached Accelerator.
*
*/
void build() {
......@@ -145,6 +169,12 @@ protected:
XACCError("Bad source string or something.\n");
}
// Execute hardware dependent IR Transformations
auto accTransforms = accelerator->getIRTransformations();
for (auto t : accTransforms) {
xaccIR = t->transform(xaccIR);
}
// Write the IR to file if the user requests it
if (runtimeOptions->exists("persist-ir")) {
auto fileStr = (*runtimeOptions)["persist-ir"];
......@@ -155,60 +185,44 @@ protected:
return;
}
public:
/**
* The Constructor, takes the Accelerator
* to execute on, and the source to compile and execute
*
* @param acc Attached Accelerator to execute
* @param sourceFile The kernel source code
*/
Program(std::shared_ptr<Accelerator> acc, const std::string& sourceFile) :
src(sourceFile), accelerator(std::move(acc)) {
}
/**
* Return an executable version of the quantum kernel
* Return an executable version of the kernel
* referenced by the kernelName string.
*
* @param name
* @param args
* @return
* @param name The name of the kernel
* @return kernel The Kernel represented by kernelName
*/
template<typename ... RuntimeArgs>
std::function<void(std::shared_ptr<AcceleratorBuffer>, RuntimeArgs...)> getKernel(
const std::string& kernelName) {
auto getKernel(
const std::string& kernelName) -> Kernel<RuntimeArgs...> {
// Build the kernel with the appropriate compiler
build();
// Create a lambda that executes the kernel on the Accelerator.
return std::move([=](std::shared_ptr<AcceleratorBuffer> buffer, RuntimeArgs... args) {
// Get the Function for the Kernel from the IR
auto kernel = xaccIR->getKernel(kernelName);
if (!xaccIR) {
build();
}
if (sizeof...(RuntimeArgs) > 0) {
// Store the runtime parameters in a tuple
auto argsTuple = std::make_tuple(args...);
return Kernel<RuntimeArgs...>(accelerator, xaccIR->getKernel(kernelName));
}
// Loop through the tuple, and add InstructionParameters
// to the parameters vector.
std::vector<InstructionParameter> parameters;
xacc::tuple_for_each(argsTuple, [&](auto value) {
parameters.push_back(InstructionParameter(value));
});
/**
* Return all Kernels that have sizeof...(RuntimeArgs)
* InstructionParameters.
* @return kernels Kernels with sizeof...(RuntimeArgs) Parameters.
*/
template<typename ... RuntimeArgs>
auto getKernels() -> std::vector<Kernel<RuntimeArgs...>> {
std::vector<Kernel<RuntimeArgs...>> kernels;
if (!xaccIR) {
build();
}
// Evaluate all Variable Parameters
kernel->evaluateVariableParameters(parameters);
for (auto k : xaccIR->getKernels()) {
if (k->nParameters() == (sizeof...(RuntimeArgs))) {
kernels.push_back(Kernel<RuntimeArgs...>(accelerator, k));
}
}
// Execute the Kernel on the Accelerator
accelerator->execute(buffer, kernel);
return;
});
return kernels;
}
};
......
......@@ -72,15 +72,10 @@ public:
("persist-ir",value<std::string>(), "Persist generated IR to provided file name.")
("load", value<std::string>(), "Load a XACC plugin at the given path")
("list-compilers", "List all available XACC Compilers")
("list-accelerators", "List all available XACC Accelerators")
("verbose-registry", "Print registry actions");
("list-accelerators", "List all available XACC Accelerators");
}
/**
* Parse the command line options. Provide a Boost options_description
* built up and provided by all available OptionsProviders. This
* method also loads all Compilers and Accelerators available
* in the XACC_INSTALL_DIR.
*/
void parse(int argc, char** argv) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment