File size: 7,719 Bytes
8649653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * host.cu — Phase 1 host driver using the CUDA Driver API
 *
 * We use the Driver API (not the Runtime API) because it lets us load
 * a .cubin file directly with cuModuleLoad(). This is how we run hand-
 * modified SASS without going through the nvcc link step.
 *
 * Driver API vs Runtime API:
 *   Runtime API: #include <cuda_runtime.h>, cudaMalloc, cudaMemcpy, kernel<<<>>>
 *   Driver API:  #include <cuda.h>, cuMemAlloc, cuMemcpy, cuLaunchKernel
 *
 * The Driver API is more verbose but gives direct control over cubin loading.
 *
 * Build:
 *   nvcc -o host host.cu -lcuda -arch=sm_86
 *
 * Usage (after building kernels/tutorial/vector_add.cubin):
 *   host.exe vector_add.sm_86.cubin          <- run original
 *   host.exe vector_add.sm_86.modified.cubin <- run hand-modified SASS
 *
 * Expected output with FADD (addition):
 *   a[0]=1.0  b[0]=10.0  c[0]=11.0  expected=11.0  OK
 *   a[1]=2.0  b[1]=20.0  c[1]=22.0  expected=22.0  OK
 *   ...
 *
 * After changing FADD -> FMUL in the .cuasm:
 *   a[0]=1.0  b[0]=10.0  c[0]=10.0  expected=10.0  OK  (1*10=10)
 *   a[1]=2.0  b[1]=20.0  c[1]=40.0  expected=40.0  OK  (2*20=40)
 *   ...
 */

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cuda.h>

#define NUM_ELEMENTS 32
#define BLOCK_SIZE   32

// Check a CUDA Driver API call and exit on failure
#define CHECK_CU(call)                                                          \
    do {                                                                        \
        CUresult cu_result = (call);                                            \
        if (cu_result != CUDA_SUCCESS) {                                        \
            const char *error_string = nullptr;                                 \
            cuGetErrorString(cu_result, &error_string);                         \
            fprintf(stderr, "CUDA Driver API error at %s:%d — %s\n",           \
                    __FILE__, __LINE__,                                          \
                    error_string ? error_string : "unknown error");             \
            exit(EXIT_FAILURE);                                                 \
        }                                                                       \
    } while (0)


int main(int argc, char **argv) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <path_to_cubin> [multiply|add]\n", argv[0]);
        fprintf(stderr, "  add      — expect c[i] = a[i] + b[i]  (default)\n");
        fprintf(stderr, "  multiply — expect c[i] = a[i] * b[i]  (after FADD->FMUL modification)\n");
        return EXIT_FAILURE;
    }

    const char *cubin_path = argv[1];
    bool expect_multiply = (argc >= 3 && strcmp(argv[2], "multiply") == 0);

    printf("=== bare-metal Phase 1: vector_add ===\n");
    printf("Cubin: %s\n", cubin_path);
    printf("Mode:  %s\n\n", expect_multiply ? "multiply (FMUL)" : "add (FADD)");

    // --- Initialize CUDA Driver ---
    CHECK_CU(cuInit(0));

    CUdevice cu_device;
    CHECK_CU(cuDeviceGet(&cu_device, 0));

    char device_name[256];
    CHECK_CU(cuDeviceGetName(device_name, sizeof(device_name), cu_device));
    printf("Device: %s\n", device_name);

    int compute_major, compute_minor;
    CHECK_CU(cuDeviceGetAttribute(&compute_major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cu_device));
    CHECK_CU(cuDeviceGetAttribute(&compute_minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cu_device));
    printf("Compute: sm_%d%d\n\n", compute_major, compute_minor);

    CUcontext cu_context;
    // CUDA 13.2: cuCtxCreate gained a CUctxCreateParams pointer (NULL = defaults).
    CHECK_CU(cuCtxCreate(&cu_context, NULL, 0, cu_device));

    // --- Load the cubin directly ---
    CUmodule cu_module;
    CUresult load_result = cuModuleLoad(&cu_module, cubin_path);
    if (load_result != CUDA_SUCCESS) {
        const char *error_string = nullptr;
        cuGetErrorString(load_result, &error_string);
        fprintf(stderr, "Failed to load cubin '%s': %s\n", cubin_path,
                error_string ? error_string : "unknown");
        fprintf(stderr, "Make sure the cubin was compiled for sm_%d%d\n",
                compute_major, compute_minor);
        return EXIT_FAILURE;
    }

    CUfunction kernel_func;
    CHECK_CU(cuModuleGetFunction(&kernel_func, cu_module, "vector_add"));
    printf("Kernel 'vector_add' loaded from cubin.\n\n");

    // --- Allocate and initialize host memory ---
    float host_a[NUM_ELEMENTS];
    float host_b[NUM_ELEMENTS];
    float host_c[NUM_ELEMENTS];

    for (int element_idx = 0; element_idx < NUM_ELEMENTS; element_idx++) {
        host_a[element_idx] = (float)(element_idx + 1);        // 1, 2, 3, ..., 32
        host_b[element_idx] = (float)(element_idx + 1) * 10.0f; // 10, 20, 30, ..., 320
        host_c[element_idx] = 0.0f;
    }

    // --- Allocate device memory ---
    CUdeviceptr device_a, device_b, device_c;
    size_t buffer_size = NUM_ELEMENTS * sizeof(float);

    CHECK_CU(cuMemAlloc(&device_a, buffer_size));
    CHECK_CU(cuMemAlloc(&device_b, buffer_size));
    CHECK_CU(cuMemAlloc(&device_c, buffer_size));

    // --- Copy input data to device ---
    CHECK_CU(cuMemcpyHtoD(device_a, host_a, buffer_size));
    CHECK_CU(cuMemcpyHtoD(device_b, host_b, buffer_size));

    // --- Launch the kernel via Driver API ---
    int num_elements = NUM_ELEMENTS;
    void *kernel_args[] = { &device_a, &device_b, &device_c, &num_elements };

    CHECK_CU(cuLaunchKernel(
        kernel_func,
        1, 1, 1,          // grid:  1 block
        BLOCK_SIZE, 1, 1, // block: 32 threads (one warp)
        0,                // shared memory bytes
        NULL,             // stream (default)
        kernel_args,
        NULL
    ));

    // Wait for kernel to complete
    CHECK_CU(cuCtxSynchronize());

    // --- Copy results back ---
    CHECK_CU(cuMemcpyDtoH(host_c, device_c, buffer_size));

    // --- Verify results ---
    printf("Results (showing first 8 elements):\n");
    printf("  %-8s %-8s %-12s %-12s %s\n", "a[i]", "b[i]", "c[i] (GPU)", "expected", "status");
    printf("  %s\n", "---------------------------------------------------");

    int num_errors = 0;
    for (int element_idx = 0; element_idx < NUM_ELEMENTS; element_idx++) {
        float expected;
        if (expect_multiply) {
            expected = host_a[element_idx] * host_b[element_idx];
        } else {
            expected = host_a[element_idx] + host_b[element_idx];
        }

        bool is_correct = (fabsf(host_c[element_idx] - expected) < 1e-3f);
        if (!is_correct) {
            num_errors++;
        }

        // Print first 8 elements + any errors
        if (element_idx < 8 || !is_correct) {
            printf("  %-8.1f %-8.1f %-12.1f %-12.1f %s\n",
                   host_a[element_idx],
                   host_b[element_idx],
                   host_c[element_idx],
                   expected,
                   is_correct ? "OK" : "MISMATCH");
        }
    }
    if (NUM_ELEMENTS > 8) {
        printf("  ... (%d more elements)\n", NUM_ELEMENTS - 8);
    }

    printf("\n");
    if (num_errors == 0) {
        printf("PASS: All %d elements correct.\n", NUM_ELEMENTS);
        if (expect_multiply) {
            printf("      FMUL modification confirmed — GPU is multiplying, not adding!\n");
        } else {
            printf("      Original FADD kernel working correctly.\n");
        }
    } else {
        printf("FAIL: %d/%d elements incorrect.\n", num_errors, NUM_ELEMENTS);
    }

    // --- Cleanup ---
    cuMemFree(device_a);
    cuMemFree(device_b);
    cuMemFree(device_c);
    cuModuleUnload(cu_module);
    cuCtxDestroy(cu_context);

    return (num_errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}