-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eric Schweitz <[email protected]>
- Loading branch information
1 parent
75b9c03
commit 57ecc8d
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/****************************************************************-*- C++ -*-**** | ||
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
namespace cudaq { | ||
|
||
/// A kernel may return results dynamically if the size of the result is not a | ||
/// constant at compile-time. | ||
struct KernelThunkResultType { | ||
void *data_buffer; ///< Pointer to the first element of an array. | ||
std::uint64_t size; ///< The size of the buffer in bytes. | ||
}; | ||
|
||
/// The universal signature of a kernel thunk. | ||
using KernelThunkType = KernelThunkResultType (*)(void *, bool); | ||
|
||
/// The degenerate form of a kernel call. In some launch cases, it may be | ||
/// predetermined that the kernel can be called without a thunk. | ||
using KernelDegenerateType = void (*)(void *); | ||
|
||
/// In some cases, the launcher will bypass the thunk function and call a | ||
/// degenerate stub. That means that the extra `bool` argument will be ignored | ||
/// by the called kernel and the kernel will not return a dynamic result. | ||
/// | ||
/// This is a terrible idea, generally speaking. However, if the launcher | ||
/// neither looks for nor attempts to use the second `bool` argument at all, and | ||
/// the launcher will drop any results returned from the kernel (regardless of | ||
/// type) on the floor anyway, then one may be able to get away with using a | ||
/// degenerate kernel type. | ||
inline KernelDegenerateType | ||
make_degenerate_kernel_type(KernelThunkType func_type) { | ||
return reinterpret_cast<KernelDegenerateType>( | ||
reinterpret_cast<void *>(func_type)); | ||
} | ||
|
||
} // namespace cudaq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
// RUN: nvq++ %cpp_std --enable-mlir %s -o %t && %t | ||
|
||
#include "cudaq.h" | ||
#include <cstdio> | ||
|
||
struct VectorBoolResult { | ||
std::vector<bool> operator()() __qpu__ { | ||
std::vector<bool> result(3); | ||
result[0] = true; | ||
result[1] = false; | ||
result[2] = true; | ||
return result; | ||
} | ||
}; | ||
|
||
struct VectorIntResult { | ||
std::vector<int> operator()() __qpu__ { | ||
std::vector<int> result(2); | ||
result[0] = 42; | ||
result[1] = -23479; | ||
return result; | ||
} | ||
}; | ||
|
||
struct VectorDoubleResult { | ||
std::vector<double> operator()() __qpu__ { | ||
std::vector<double> result(2); | ||
result[0] = 543.0; | ||
result[1] = -234234.0; | ||
return result; | ||
} | ||
}; | ||
|
||
int main() { | ||
auto retb{VectorBoolResult{}()}; | ||
printf("%d %d %d\n", static_cast<int>(retb[0]), static_cast<int>(retb[1]), | ||
static_cast<int>(retb[2])); | ||
auto ret = VectorIntResult{}(); | ||
printf("%d %d\n", ret[0], ret[1]); | ||
std::vector<double> retd{VectorDoubleResult{}()}; | ||
printf("%f %f\n", retd[0], retd[1]); | ||
return !(retb[0] && !retb[1] && retb[2] && ret[0] == 42 && ret[1] == -23479 && | ||
retd[0] == 543.0 && retd[1] == -234234.0); | ||
} |