File size: 3,106 Bytes
dffe123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#version 450
// #extension GL_EXT_debug_printf : enable
#define ALL_THREAD 1024
// #define ALL_THREAD 128 // Experimental batched operation
#define STEP_SIZE 65536

layout(binding = 0) readonly buffer Input1{
    float matA[];
};

layout(binding = 1) readonly buffer Input2{
    float matB[];
};

layout(binding = 2) writeonly buffer Output{
    float matOut[];
};

layout(binding = 3) uniform Params {
    int opType;
    int ndims;
} params;

layout(binding = 4) readonly buffer Shape {
    int shape[];
};

layout(binding = 5) readonly buffer Step {
    int matStep[];
};

/* local_size_x, local_size_y, local_size_z there defines the number of invocations
   of this compute shader in the current work group. */
// TODO: Check if this makes any sense
// TODO: Check if it is required to fetch PhysicalDeviceLimit from Context
// TODO: here we shall assume that maxGroupInvocation is 1024.
layout(local_size_x = ALL_THREAD, local_size_y = 1, local_size_z = 1) in; // TODO: Check if this makes any sense

const int AND = 0;
const int EQUAL = 1;
const int GREATER = 2;
const int GREATER_EQUAL = 3;
const int LESS = 4;
const int LESS_EQUAL = 5;
const int OR = 6;
const int POW = 7;
const int XOR = 8;
const int BITSHIFT = 9;
const int MAX = 10;
const int MEAN = 11;
const int MIN = 12;
const int MOD = 13;
const int FMOD = 14;
const int PROD = 15;
const int SUB = 16;
const int SUM = 17;
const int ADD = 18;
const int DIV = 19;
const int WHERE = 20;

void binary_forward()
{
    int ndims = params.ndims;
    int dp1 = matStep[2 * ndims - 1];
    int dp2 = matStep[3 * ndims - 1];
    int dp = matStep[ndims - 1];
    int n1 = shape[ndims - 1], n2 = shape[ndims - 2];

    int plane_idx = int(gl_WorkGroupID.x);

    int ptr1 = 0;
    int ptr2 = 0;
    int ptr = 0;
    int idx = plane_idx;

    for (int k = ndims - 3; k >= 0; --k) {
        int next_idx = idx / shape[k];
        int i_k = idx - next_idx * shape[k]; // i_k = idx % shape[k]
        ptr1 += i_k * matStep[ndims + k];
        ptr2 += i_k * matStep[2 * ndims + k];
        ptr += i_k * matStep[k];
        idx = next_idx;
    }

    int i2_offset = int(gl_WorkGroupID.y);
    int i1_offset = int(gl_LocalInvocationID.x);

    ptr1 += i2_offset * matStep[2 * ndims - 2];
    ptr2 += i2_offset * matStep[3 * ndims - 2];
    ptr += i2_offset * matStep[ndims - 2];

    for (int i1 = i1_offset; i1 < n1; i1 += ALL_THREAD) {
        switch (params.opType) {
            case int(ADD):
                matOut[ptr + i1 * dp] = matA[ptr1 + i1 * dp1] + matB[ptr2 + i1 * dp2];
                break;
            case int(SUB):
                matOut[ptr + i1 * dp] = matA[ptr1 + i1 * dp1] - matB[ptr2 + i1 * dp2];
                break;
            case int(PROD):
                matOut[ptr + i1 * dp] = matA[ptr1 + i1 * dp1] * matB[ptr2 + i1 * dp2];
                break;
            case int(DIV):
                matOut[ptr + i1 * dp] = matA[ptr1 + i1 * dp1] / matB[ptr2 + i1 * dp2];
                break;
        }
    }
}


void main()
{
    // debugPrintfEXT("nary_eltwise_binary_forward.comp loaded\n");
    binary_forward();
    return;
}