summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-08-21 18:31:49 +0200
committerTyge Løvset <[email protected]>2023-08-21 18:31:49 +0200
commit2d33308d36063f3726f3652b0b0cbe3668b8bc68 (patch)
tree2f3750a00190e48682b9082a36a5085defa128e0
parent7b57eb4240ee886278b862ed8c90618376237afc (diff)
downloadSTC-modified-2d33308d36063f3726f3652b0b0cbe3668b8bc68.tar.gz
STC-modified-2d33308d36063f3726f3652b0b0cbe3668b8bc68.zip
Changed 'order' to 'layout' in cspan md.
Neigher the 'C' / 'F' convension from python, nor left / right from std::mdspan are great names => changed to c_ROWMAJOR / c_COLMAJOR like in matlab.
-rw-r--r--docs/cspan_api.md18
-rw-r--r--include/stc/cspan.h23
-rw-r--r--misc/examples/spans/matmult.c4
-rw-r--r--misc/examples/spans/mdspan.c2
4 files changed, 28 insertions, 19 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md
index 39b97473..c3556dc3 100644
--- a/docs/cspan_api.md
+++ b/docs/cspan_api.md
@@ -44,12 +44,15 @@ SpanTypeN_iter SpanType_begin(const SpanTypeN* self);
SpanTypeN_iter SpanType_end(const SpanTypeN* self);
void SpanType_next(SpanTypeN_iter* it);
-SpanTypeN cspan_md(ValueType* data, d1, d2, ...); // make a multi-dim cspan, row-major order.
-SpanTypeN cspan_md_order(char order, ValueType* data, d1, d2, ...); // order='C': row-major, 'F': column-major (FORTRAN).
+ // make a multi-dim cspan
+SpanTypeN cspan_md(ValueType* data, d1, d2, ...); // row-major
+SpanTypeN cspan_md_layout(cspan_layout layout, ValueType* data, d1, d2, ...);
- // transpose a md span (inverse axes). No changes to the underlying array.
+ // transpose a md span. Inverses layout and axes only.
void cspan_transpose(const SpanTypeN* self);
-bool cspan_is_order_F(const SpanTypeN* self);
+cspan_layout cspan_get_layout(const SpanTypeN* self);
+bool cspan_is_rowmajor(const SpanTypeN* self);
+bool cspan_is_colmajor(const SpanTypeN* self);
// create a subspan of input span rank. Like e.g. cspan_slice(Span3, &ms3, {off,off+count}, {c_ALL}, {c_ALL});
SpanType cspan_subspan(const SpanType* span, intptr_t offset, intptr_t count);
@@ -70,8 +73,9 @@ OutSpanN cspan_slice(TYPE OutSpanN, const SpanTypeM* parent, {x0,x1}, {y0
|:------------------|:----------------------------------------------------|:---------------------|
| SpanTypeN | `struct { ValueType *data; uint32_t shape[N]; .. }` | SpanType with rank N |
| SpanTypeN`_value` | `ValueType` | The ValueType |
-| `c_ALL` | Use with `cspan_slice()`. | Full extent |
-| `c_END` | " | End of extent |
+| `c_ALL` | `cspan_slice(&md, {1,3}, {c_ALL})` | Full extent |
+| `c_END` | `cspan_slice(&md, {1,c_END}, {2,c_END})` | End of extent |
+| `cspan_layout` | `enum { c_ROWMAJOR, c_COLMAJOR }` | Multi-dim layout |
## Example 1
@@ -182,7 +186,7 @@ void print_span(myspan2 ms) {
int main(void) {
int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
- myspan3 ms3 = cspan_md_order('C', arr, 2, 3, 4); // row-major ('F' column-major)
+ myspan3 ms3 = cspan_md(arr, 2, 3, 4); // row-major layout
myspan3 ss3 = cspan_slice(myspan3, &ms3, {c_ALL}, {0,3}, {2,c_END});
myspan2 a = cspan_submd3(&ss3, 1);
myspan2 b = a;
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index 6f8de8ec..f9f3b02a 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -98,7 +98,7 @@ int demo2() {
} \
STC_INLINE void Self##_next(Self##_iter* it) { \
int i, inc, done; \
- if (it->_s->stride.d[0] < it->_s->stride.d[RANK - 1]) i=0, inc=1; else i=RANK-1, inc=-1; \
+ if (cspan_is_colmajor(it->_s)) i=0, inc=1; else i=RANK-1, inc=-1; \
it->ref += _cspan_next##RANK(it->pos, it->_s->shape, it->_s->stride.d, RANK, i, inc, &done); \
if (done) it->ref = NULL; \
} \
@@ -115,6 +115,7 @@ using_cspan_tuple(7); using_cspan_tuple(8);
#define c_END -1
#define c_ALL 0,c_END
+typedef enum {c_ROWMAJOR, c_COLMAJOR} cspan_layout;
/* Use cspan_init() for static initialization only. c_init() for non-static init. */
#define cspan_init(SpanType, ...) \
@@ -132,7 +133,9 @@ using_cspan_tuple(7); using_cspan_tuple(8);
#define cspan_size(self) _cspan_size((self)->shape, cspan_rank(self))
#define cspan_rank(self) c_arraylen((self)->shape)
-#define cspan_is_order_F(self) ((self)->stride.d[0] < (self)->stride.d[cspan_rank(self) - 1])
+#define cspan_is_colmajor(self) ((self)->stride.d[0] < (self)->stride.d[cspan_rank(self) - 1])
+#define cspan_is_rowmajor(self) (!cspan_is_colmajor(self))
+#define cspan_get_layout(self) (cspan_is_colmajor(self) ? c_COLMAJOR : c_ROWMAJOR)
#define cspan_index(self, ...) c_PASTE(cspan_idx_, c_NUMARGS(__VA_ARGS__))(self, __VA_ARGS__)
#define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__))
#define cspan_front(self) ((self)->data)
@@ -165,10 +168,10 @@ using_cspan_tuple(7); using_cspan_tuple(8);
#define cspan_submd4_4(self, x, y, z) \
{.data=cspan_at(self, x, y, z, 0), .shape={(self)->shape[3]}, .stride=(cspan_tuple1){.d={(self)->stride.d[3]}}}
-#define cspan_md(array, ...) cspan_md_order('C', array, __VA_ARGS__)
-#define cspan_md_order(order, array, ...) /* order='C' or 'F' */ \
+#define cspan_md(array, ...) cspan_md_layout(c_ROWMAJOR, array, __VA_ARGS__)
+#define cspan_md_layout(layout, array, ...) \
{.data=array, .shape={__VA_ARGS__}, \
- .stride=*(c_PASTE(cspan_tuple, c_NUMARGS(__VA_ARGS__))*)_cspan_shape2stride(order, ((int32_t[]){__VA_ARGS__}), c_NUMARGS(__VA_ARGS__))}
+ .stride=*(c_PASTE(cspan_tuple, c_NUMARGS(__VA_ARGS__))*)_cspan_shape2stride(layout, ((int32_t[]){__VA_ARGS__}), c_NUMARGS(__VA_ARGS__))}
#define cspan_transpose(self) \
_cspan_transpose((self)->shape, (self)->stride.d, cspan_rank(self))
@@ -225,7 +228,7 @@ STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t shape[], const int32_t s
}
STC_API intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_t stride[], int rank, int i, int inc, int* done);
-#define _cspan_next1(pos, shape, stride, rank, i, inc, done) (*done = ++pos[0]==shape[0], stride[0])
+#define _cspan_next1(pos, shape, stride, rank, i, inc, done) (*done = ++pos[0]==shape[0], (void)(i|inc), stride[0])
#define _cspan_next3 _cspan_next2
#define _cspan_next4 _cspan_next2
#define _cspan_next5 _cspan_next2
@@ -237,7 +240,7 @@ STC_API intptr_t _cspan_slice(int32_t oshape[], int32_t ostride[], int* orank,
const int32_t shape[], const int32_t stride[],
int rank, const int32_t a[][2]);
-STC_API int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank);
+STC_API int32_t* _cspan_shape2stride(cspan_layout layout, int32_t shape[], int rank);
#endif // STC_CSPAN_H_INCLUDED
/* --------------------- IMPLEMENTATION --------------------- */
@@ -246,6 +249,7 @@ STC_API int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank);
STC_DEF intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_t stride[], int rank, int i, int inc, int* done) {
intptr_t off = stride[i];
++pos[i];
+
while (--rank && pos[i] == shape[i]) {
pos[i] = 0; ++pos[i + inc];
off += stride[i + inc] - stride[i]*shape[i];
@@ -255,9 +259,9 @@ STC_DEF intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_
return off;
}
-STC_DEF int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank) {
+STC_DEF int32_t* _cspan_shape2stride(cspan_layout layout, int32_t shape[], int rank) {
int i, inc;
- if (order == 'F') i = 0, inc = 1;
+ if (layout == c_COLMAJOR) i = 0, inc = 1;
else i = rank - 1, inc = -1;
int32_t k = 1, s1 = shape[i], s2;
@@ -277,6 +281,7 @@ STC_DEF intptr_t _cspan_slice(int32_t oshape[], int32_t ostride[], int* orank,
intptr_t off = 0;
int i = 0, oi = 0;
int32_t end;
+
for (; i < rank; ++i) {
off += stride[i]*a[i][0];
switch (a[i][1]) {
diff --git a/misc/examples/spans/matmult.c b/misc/examples/spans/matmult.c
index 35dad7a9..266fa121 100644
--- a/misc/examples/spans/matmult.c
+++ b/misc/examples/spans/matmult.c
@@ -70,8 +70,8 @@ int main(void)
Values_push(&values, (crandf() - 0.5)*4.0);
double out[D1*D2];
- Mat3 data = cspan_md_order('C', values.data, N, D1, D2);
- OutMat c = cspan_md_order('C', out, D1, D2);
+ Mat3 data = cspan_md_layout(c_ROWMAJOR, values.data, N, D1, D2);
+ OutMat c = cspan_md_layout(c_ROWMAJOR, out, D1, D2);
Mat2 a = cspan_submd3(&data, 0);
double sum = 0.0;
clock_t t = clock();
diff --git a/misc/examples/spans/mdspan.c b/misc/examples/spans/mdspan.c
index db601850..630ffddb 100644
--- a/misc/examples/spans/mdspan.c
+++ b/misc/examples/spans/mdspan.c
@@ -9,7 +9,7 @@ int main(void) {
double* data = c_new_n(double, nx*ny*nz);
printf("\nMultidim span ms[5, 4, 3], fortran ordered");
- DSpan3 ms = cspan_md_order('F', data, nx, ny, nz); // Fortran, not 'C'
+ DSpan3 ms = cspan_md_layout(c_COLMAJOR, data, nx, ny, nz);
int idx = 0;
for (int i = 0; i < ms.shape[0]; ++i)