summaryrefslogtreecommitdiffstats
path: root/tools/cuda-ab/p2/src/common.h
blob: e1e31377dd7f1b4871d0eb87d1a195b6c6e5c853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: BSD-3-Clause
/*
 * common.h - shared conventions for the P2 "arbitrary CUDA kernels" suite.
 *
 * Line conventions (p2/run_guest.sh filters on these):
 *
 *   compared (stable)   RESULT / CHECK / ERR / STATUS / P2PRINT / PROBE / MAIL
 *   informational       DEVICE / INFO / TIMING / MEMINFO   (ignored by compare)
 *
 * Every program accepts --emit-expected: it prints the deterministic
 * machine-checkable lines a correct GPU run must produce, computed on the
 * CPU only, without touching CUDA. p2/oracle.sh uses this fallback when the
 * host has no usable GPU (e.g. the device is owned by a running guest), so
 * the expected/ references are a documented deterministic computation
 * rather than a captured bare-metal run.
 *
 * All verification in this suite is exact (integers, or bit-identical
 * float/integer arithmetic and correctly-rounded fmaf), so `bad=0` is
 * meaningful and no tolerances are used.
 */
#ifndef P2_COMMON_H
#define P2_COMMON_H

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <cuda_runtime.h>

#define P2_CHECK(call)                                                        \
	do {                                                                  \
		cudaError_t e_ = (call);                                      \
		if (e_ != cudaSuccess) {                                      \
			printf("ERR cuda=%s code=%d line=%d\n",               \
			       cudaGetErrorString(e_), (int)e_, __LINE__);    \
			printf("STATUS FAIL\n");                              \
			fflush(stdout);                                       \
			return 1;                                             \
		}                                                             \
	} while (0)

static inline int p2_emit_mode(int argc, char **argv)
{
	return (argc > 1 && strcmp(argv[1], "--emit-expected") == 0) ? 1 : 0;
}

static inline void p2_device_info(void)
{
	cudaDeviceProp prop;

	if (cudaGetDeviceProperties(&prop, 0) == cudaSuccess)
		printf("DEVICE %s cc=%d.%d sm=%d mem=%llu\n", prop.name,
		       prop.major, prop.minor, prop.multiProcessorCount,
		       (unsigned long long)prop.totalGlobalMem);
	else
		printf("DEVICE unavailable\n");
}

static inline int p2_status(int bad)
{
	printf("STATUS %s\n", bad == 0 ? "OK" : "FAIL");
	fflush(stdout);
	return bad == 0 ? 0 : 1;
}

#endif /* P2_COMMON_H */