Created
September 18, 2017 07:36
-
-
Save mratsim/1eae17b18b210f58c99c27560617fca3 to your computer and use it in GitHub Desktop.
Convert element ID to Offset in strided array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: this doesn't work (yet) | |
// Example program | |
#include <iostream> | |
#include <string> | |
int cuda_getIndexOfElementID( | |
const int rank, | |
const int * __restrict__ shape, | |
const int * __restrict__ strides, | |
const int offset, | |
const int element_id) { | |
int result = offset; | |
int reminderOffset = element_id; | |
int dimIndex = 0; | |
for (int k = rank - 1; k = 0; --k) { | |
dimIndex = reminderOffset % shape[k]; | |
reminderOffset /= shape[k]; | |
result += dimIndex * strides[k]; | |
} | |
} | |
int main() | |
{ | |
int rank = 2; | |
int shape[2] = {8, 3}; | |
int strides[2] = {3,1}; | |
int offset = 0; | |
int element_id = 5; | |
int result = cuda_getIndexOfElementID(rank, &shape[0], &strides[0], offset, element_id); | |
std::cout << "Result is: " << result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment