stdgpu/contract.h Source File

stdgpu/contract.h Source File#

stdgpu: stdgpu/contract.h Source File
stdgpu Latest
Efficient STL-like Data Structures on the GPU
contract.h
Go to the documentation of this file.
1/*
2 * Copyright 2019 Patrick Stotko
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef STDGPU_CONTRACT_H
17#define STDGPU_CONTRACT_H
18
28#include <cassert>
29#include <cstdio>
30#include <exception>
31
32#include <stdgpu/compiler.h>
33#include <stdgpu/config.h>
34#include <stdgpu/cstddef.h>
35#include <stdgpu/platform.h>
36
38// NOTE: CUDA-Clang uses merged parsing and needs a device version of std::terminate
39#if STDGPU_DEVICE_COMPILER == STDGPU_DEVICE_COMPILER_CUDACLANG
40namespace std
41{
42STDGPU_CUDA_DEVICE_ONLY void
43terminate()
44{
45 // Dummy function for parsing only
46}
47} // namespace std
48#endif
50
51namespace stdgpu
52{
53
71#define STDGPU_DETAIL_EMPTY_STATEMENT (void)0
73
74#if STDGPU_ENABLE_CONTRACT_CHECKS
75
76 #define STDGPU_DETAIL_HOST_CHECK(type, ...) \
77 if (!(__VA_ARGS__)) \
78 { \
79 printf("stdgpu : " type " failure :\n" \
80 " File : %s:%d\n" \
81 " Function : %s\n" \
82 " Condition : %s\n", \
83 __FILE__, \
84 __LINE__, \
85 static_cast<const char*>(STDGPU_FUNC), \
86 #__VA_ARGS__); \
87 std::terminate(); \
88 } \
89 STDGPU_DETAIL_EMPTY_STATEMENT
90
91 #define STDGPU_DETAIL_HOST_EXPECTS(...) STDGPU_DETAIL_HOST_CHECK("Precondition", __VA_ARGS__)
92 #define STDGPU_DETAIL_HOST_ENSURES(...) STDGPU_DETAIL_HOST_CHECK("Postcondition", __VA_ARGS__)
93 #define STDGPU_DETAIL_HOST_ASSERT(...) STDGPU_DETAIL_HOST_CHECK("Assertion", __VA_ARGS__)
94
95 #define STDGPU_DETAIL_DEVICE_CHECK(...) assert((__VA_ARGS__)) // NOLINT(hicpp-no-array-decay)
96
97 #define STDGPU_DETAIL_DEVICE_EXPECTS(...) STDGPU_DETAIL_DEVICE_CHECK(__VA_ARGS__)
98 #define STDGPU_DETAIL_DEVICE_ENSURES(...) STDGPU_DETAIL_DEVICE_CHECK(__VA_ARGS__)
99 #define STDGPU_DETAIL_DEVICE_ASSERT(...) STDGPU_DETAIL_DEVICE_CHECK(__VA_ARGS__)
100
101 #if STDGPU_CODE == STDGPU_CODE_DEVICE
102 #define STDGPU_EXPECTS(...) STDGPU_DETAIL_DEVICE_EXPECTS(__VA_ARGS__)
103 #define STDGPU_ENSURES(...) STDGPU_DETAIL_DEVICE_ENSURES(__VA_ARGS__)
104 #define STDGPU_ASSERT(...) STDGPU_DETAIL_DEVICE_ASSERT(__VA_ARGS__)
105 #else
106 #define STDGPU_EXPECTS(...) STDGPU_DETAIL_HOST_EXPECTS(__VA_ARGS__)
107 #define STDGPU_ENSURES(...) STDGPU_DETAIL_HOST_ENSURES(__VA_ARGS__)
108 #define STDGPU_ASSERT(...) STDGPU_DETAIL_HOST_ASSERT(__VA_ARGS__)
109 #endif
110#else
111 #define STDGPU_EXPECTS(...) STDGPU_DETAIL_EMPTY_STATEMENT
112 #define STDGPU_ENSURES(...) STDGPU_DETAIL_EMPTY_STATEMENT
113 #define STDGPU_ASSERT(...) STDGPU_DETAIL_EMPTY_STATEMENT
114#endif
115
116} // namespace stdgpu
117
118#endif // STDGPU_CONTRACT_H