CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "omega_vector.h" |
| 18 | #include "omega_core.h" |
| 19 | #include "omega_canonical.h" |
| 20 | #include "sha256.h" |
| 21 | #include <string.h> |
| 22 | |
| 23 | #ifndef CUDAPP_MIGRATION_PENDING_C_CORE |
| 24 | #include "omega_core_binding.h" |
| 25 | #include <stdlib.h> |
| 26 | _Static_assert(sizeof(OmegaVectorSpec)==144 && offsetof(OmegaVectorSpec,element_count)==104, "vector bootstrap ABI"); |
| 27 | static void vec_status(uint32_t s) { if(s!=OMEGA_OK)CORE_STOP(); } |
| 28 | int omega_vector_spec_init(OmegaVectorSpec *s,const char *name,uint32_t n) { |
| 29 | if(!s||!name)return -1; |
| 30 | char nm[64]={0}; strncpy(nm,name,63); uint64_t r; |
| 31 | vec_status(CORE_CALL(omvspec, &r,s,sizeof *s,nm,sizeof nm,n)); |
| 32 | if(r)return (int)(int64_t)r; |
| 33 | sha256_ctx c;sha256_init(&c); |
| 34 | sha256_update(&c,(const uint8_t *)"OMEGA_VECADD_U32",16); |
| 35 | sha256_update(&c,(const uint8_t *)&s->element_width,sizeof s->element_width); |
| 36 | sha256_update(&c,(const uint8_t *)&s->element_count,sizeof s->element_count); |
| 37 | uint8_t ov=(uint8_t)s->overflow;sha256_update(&c,&ov,1); |
| 38 | sha256_final(&c,s->spec_digest);memcpy(s->spec_id.bytes,s->spec_digest,32);return 0; |
| 39 | } |
| 40 | static int vec_partial_overlap(const void *a,const void *b,uint32_t n) { |
| 41 | uintptr_t x=(uintptr_t)a,y=(uintptr_t)b; |
| 42 | return x!=y&&(x<y?y-x:x-y)<(uint64_t)n*4; |
| 43 | } |
| 44 | static int vec_batch(const uint32_t *a,const uint32_t *b,uint32_t *c,uint32_t n,size_t *idx,uint64_t mode) { |
| 45 | uint32_t av[1024],bv[1024],cv[1024]; |
| 46 | int sequential=mode!=1&&(vec_partial_overlap(a,c,n)||vec_partial_overlap(b,c,n)|| |
| 47 | (mode==2&&vec_partial_overlap(a,b,n))); |
| 48 | for(uint64_t base=0;base<n;) { |
| 49 | size_t count=sequential?1:n-base>1024?1024:n-base; |
| 50 | if(mode!=2){memcpy(av,a+base,count*4);memcpy(bv,b+base,count*4);} |
| 51 | if(mode==1)memcpy(cv,c+base,count*4); |
| 52 | uint64_t r;vec_status(CORE_CALL(omvec, &r,av,sizeof av,bv,sizeof bv,cv,sizeof cv,count,base,mode)); |
| 53 | if(mode==1&&r!=1024){if(idx)*idx=base+r;return -1;} |
| 54 | if(mode==2){memcpy((uint32_t *)a+base,av,count*4);memcpy((uint32_t *)b+base,bv,count*4);} |
| 55 | if(mode!=1)memcpy(c+base,cv,count*4); |
| 56 | base+=count; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | void omega_vector_oracle_u32(const uint32_t *a,const uint32_t *b,uint32_t *c,uint32_t n) { |
| 61 | if(a&&b&&c&&n)vec_batch(a,b,c,n,NULL,0); |
| 62 | } |
| 63 | int omega_vector_verify_oracle(const uint32_t *a,const uint32_t *b,const uint32_t *c,uint32_t n,size_t *idx) { |
| 64 | if(!a||!b||!c||!n)return -1; |
| 65 | return vec_batch(a,b,(uint32_t *)c,n,idx,1); |
| 66 | } |
| 67 | void omega_vector_generate_deterministic(uint32_t *a,uint32_t *b,uint32_t *c,uint32_t n) { |
| 68 | if(a&&b&&c&&n)vec_batch(a,b,c,n,NULL,2); |
| 69 | } |
| 70 | #else |
| 71 | int omega_vector_spec_init(OmegaVectorSpec *spec, const char *name, uint32_t n) { |
| 72 | if (!spec || !name || n == 0) return -1; |
| 73 | memset(spec, 0, sizeof(*spec)); |
| 74 | strncpy(spec->name, name, sizeof(spec->name) - 1); |
| 75 | spec->element_type = TYPE_UNSIGNED_INT; |
| 76 | spec->element_width = 32; |
| 77 | spec->element_count = n; |
| 78 | spec->overflow = OVERFLOW_WRAP; |
| 79 | |
| 80 | /* Canonical spec digest: SHA-256("OMEGA_VECADD_U32" || N || overflow) */ |
| 81 | sha256_ctx ctx; |
| 82 | sha256_init(&ctx); |
| 83 | sha256_update(&ctx, (const uint8_t *)"OMEGA_VECADD_U32", 16); |
| 84 | sha256_update(&ctx, (const uint8_t *)&spec->element_width, sizeof(spec->element_width)); |
| 85 | sha256_update(&ctx, (const uint8_t *)&spec->element_count, sizeof(spec->element_count)); |
| 86 | uint8_t ov = (uint8_t)spec->overflow; |
| 87 | sha256_update(&ctx, &ov, sizeof(ov)); |
| 88 | sha256_final(&ctx, spec->spec_digest); |
| 89 | |
| 90 | memcpy(spec->spec_id.bytes, spec->spec_digest, OMEGA_ID_BYTES); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | void omega_vector_oracle_u32(const uint32_t *a, const uint32_t *b, uint32_t *c, uint32_t n) { |
| 95 | if (!a || !b || !c || n == 0) return; |
| 96 | for (uint32_t i = 0; i < n; i++) { |
| 97 | c[i] = (uint32_t)(a[i] + b[i]); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int omega_vector_verify_oracle(const uint32_t *a, const uint32_t *b, const uint32_t *actual_c, |
| 102 | uint32_t n, size_t *first_mismatch_idx) { |
| 103 | if (!a || !b || !actual_c || n == 0) return -1; |
| 104 | for (size_t i = 0; i < n; i++) { |
| 105 | uint32_t expected = (uint32_t)(a[i] + b[i]); |
| 106 | if (actual_c[i] != expected) { |
| 107 | if (first_mismatch_idx) *first_mismatch_idx = i; |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | void omega_vector_generate_deterministic(uint32_t *a, uint32_t *b, uint32_t *c_poison, uint32_t n) { |
| 115 | if (!a || !b || !c_poison || n == 0) return; |
| 116 | for (uint32_t i = 0; i < n; i++) { |
| 117 | /* Deterministic vectors exercising small values, offsets, and high-bit modulo wrapping */ |
| 118 | if (i == 0) { |
| 119 | a[i] = 0xFFFFFFFEU; |
| 120 | b[i] = 3U; /* modulo 2^32 wrap check: 0xFFFFFFFE + 3 = 1 */ |
| 121 | } else if (i == 1) { |
| 122 | a[i] = 0x80000000U; |
| 123 | b[i] = 0x80000000U; /* modulo 2^32 wrap check: 2^31 + 2^31 = 0 */ |
| 124 | } else { |
| 125 | a[i] = 1000U + i; |
| 126 | b[i] = 2000U + i * 2; |
| 127 | } |
| 128 | c_poison[i] = OMEGA_VECTOR_POISON_VALUE; |
| 129 | } |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | int omega_vector_build_spec_graph(OmegaGraph *g, uint32_t n, SemanticId *out_spec_id) { |
| 134 | if (!g || n == 0 || !out_spec_id) return -1; |
| 135 | |
| 136 | OmegaObject *t_u32 = omega_build_type_uint(g, 32); |
| 137 | if (!t_u32) return -1; |
| 138 | |
| 139 | OmegaObject *t_seq = omega_build_type_sequence(g, &t_u32->id, n); |
| 140 | if (!t_seq) return -1; |
| 141 | |
| 142 | OmegaObject *op_add = omega_build_op_binary(g, OP_ADD, OVERFLOW_WRAP, &t_u32->id); |
| 143 | if (!op_add) return -1; |
| 144 | |
| 145 | OmegaObject *spec_obj = omega_graph_add_object(g, KIND_OPERATION); |
| 146 | if (!spec_obj) return -1; |
| 147 | |
| 148 | omega_object_add_attribute(spec_obj, "name", (const uint8_t *)"omega_blackwell_vector_add", 26); |
| 149 | omega_object_add_relation(spec_obj, REL_DERIVED_FROM, &op_add->id); |
| 150 | omega_object_add_relation(spec_obj, REL_DEPENDS_ON, &t_seq->id); |
| 151 | |
| 152 | #ifndef CUDAPP_MIGRATION_PENDING_C_CORE |
| 153 | vec_status(CORE_VOID(omvpay,spec_obj,sizeof *spec_obj,n)); |
| 154 | #else |
| 155 | uint8_t payload[8]; |
| 156 | memcpy(payload, &n, sizeof(n)); |
| 157 | uint32_t elem_sz = 4; |
| 158 | memcpy(payload + 4, &elem_sz, sizeof(elem_sz)); |
| 159 | memcpy(spec_obj->payload, payload, sizeof(payload)); |
| 160 | spec_obj->payload_len = sizeof(payload); |
| 161 | #endif |
| 162 | |
| 163 | if (omega_compute_semantic_id(spec_obj) != 0) return -1; |
| 164 | *out_spec_id = spec_obj->id; |
| 165 | return 0; |
| 166 | } |