interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / evidence / 2026-11-06-costmodel-rest / rx_cm_sort.h.gcov

77 lines · 4076 bytes Raw Blame History
1 -: 0:Source:src/runtime/rx_cm_sort.h
2 -: 0:Graph:/tmp/cm-full-cov-v5qvhlky/oracle.gcno
3 -: 0:Data:/tmp/cm-full-cov-v5qvhlky/oracle.gcda
4 -: 0:Runs:2
5 -: 1:/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
6 -: 2:#ifndef RX_CM_SORT_H
7 -: 3:#define RX_CM_SORT_H
8 -: 4:#include "cudapp_native.h"
9 -: 5:#include <stdlib.h>
10 -: 6:#include <string.h>
11 -: 7:#ifndef CUDAPP_MIGRATION_PENDING_C_CM_SORT
12 -: 8:#define CUDAPP_MIGRATION_PENDING_C_CM_SORT 0
13 -: 9:#endif
14 -: 10:#if CUDAPP_MIGRATION_PENDING_C_CM_SORT
15 -: 11:/* Total comparator: resolves formerly unspecified NaN and signed-zero ties. */
16function rx_cm_cmp called 2083 returned 100% blocks executed 67%
17 2083: 12:static int rx_cm_cmp(const void *pa, const void *pb) {
18 -: 13: double a, b; uint64_t ab, bb;
19 2083: 14: memcpy(&a,pa,8); memcpy(&b,pb,8); memcpy(&ab,pa,8); memcpy(&bb,pb,8);
20 2083*: 15: if (a != a) { if (b == b) return 1; }
21branch 0 taken 0 (fallthrough)
22branch 1 taken 2083
23branch 2 never executed (fallthrough)
24branch 3 never executed
25 2083*: 16: else { if (b != b) return -1; if (a < b) return -1; if (a > b) return 1; }
26branch 0 taken 0 (fallthrough)
27branch 1 taken 2083
28branch 2 taken 994 (fallthrough)
29branch 3 taken 1089
30branch 4 taken 1089 (fallthrough)
31branch 5 taken 0
32 #####: 17: return (ab > bb) - (ab < bb);
33 -: 18:}
34 -: 19:#endif
35function rx_cm_sort called 1 returned 100% blocks executed 100%
36 1: 20:static inline int rx_cm_sort(double *values, size_t n) {
37 -: 21:#if CUDAPP_MIGRATION_PENDING_C_CM_SORT
38 1: 22: qsort(values,n,sizeof *values,rx_cm_cmp); return 0;
39call 0 returned 1
40 -: 23:#else
41 -: 24: if (n < 2) return 0;
42 -: 25: if (n > SIZE_MAX / sizeof *values) return -1;
43 -: 26: uint64_t left[256]={0}, right[256]={0}, out[256]={0}, meta[4]={0}, done;
44 -: 27: for (size_t i=0;i<n;i+=256) {
45 -: 28: size_t count=n-i<256?n-i:256;
46 -: 29: memcpy(left,values+i,count*sizeof *values);meta[0]=count;meta[1]=0;meta[2]=0;
47 -: 30: if (omega_cm_sort(&done,left,256,right,256,out,256,meta,4) || done!=count) return -1;
48 -: 31: memcpy(values+i,left,count*sizeof *values);
49 -: 32: }
50 -: 33: if (n <= 256) return 0;
51 -: 34: double *scratch = malloc(n * sizeof *values);
52 -: 35: if (!scratch) return -1;
53 -: 36: double *src=values,*dst=scratch;
54 -: 37: for (size_t width=256;width<n;) {
55 -: 38: for (size_t start=0;start<n;) {
56 -: 39: size_t mid=n-start<width?n:start+width;
57 -: 40: size_t end=n-mid<width?n:mid+width;
58 -: 41: size_t a=start,b=mid,k=start;
59 -: 42: while(a<mid || b<end) {
60 -: 43: size_t na=mid-a<256?mid-a:256,nb=end-b<256?end-b:256;
61 -: 44: memcpy(left,src+a,na*sizeof *values);memcpy(right,src+b,nb*sizeof *values);
62 -: 45: meta[0]=na;meta[1]=nb;meta[2]=1;
63 -: 46: if (omega_cm_sort(&done,left,256,right,256,out,256,meta,4) || !done || done>256 || meta[2]>na || meta[3]>nb || done!=meta[2]+meta[3]) { free(scratch);return -1; }
64 -: 47: memcpy(dst+k,out,(size_t)done*sizeof *values);
65 -: 48: a+=(size_t)meta[2];b+=(size_t)meta[3];k+=(size_t)done;
66 -: 49: }
67 -: 50: start=end;
68 -: 51: }
69 -: 52: double *swap=src;src=dst;dst=swap;
70 -: 53: if(width>n/2)break;
71 -: 54: width*=2;
72 -: 55: }
73 -: 56: if(src!=values)memcpy(values,src,n*sizeof *values);
74 -: 57: free(scratch);return 0;
75 -: 58:#endif
76 -: 59:}
77 -: 60:#endif