Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion tdishr/TdiReshape.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <mds_stdarg.h>
#include <mdsdescrip.h>
#include <tdishr.h>
#include <mdsshr.h>

#include <string.h>

Expand All @@ -49,7 +50,72 @@ int Tdi1Reshape(opcode_t opcode, int narg, struct descriptor *list[],
default:
return TdiNO_OPC;
case OPC_RESHAPE:
return TdiNO_OPC;
{
uint32_t shape[MAX_DIMS] = { 0 };
mdsdsc_a_t dsc_shape = {
.length = sizeof(uint32_t),
.class = CLASS_A,
.dtype = DTYPE_LU,
.arsize = sizeof(shape),
.pointer = (char *)shape,
};

RETURN_IF_NOT_OK(TdiConvert((mdsdsc_a_t *)list[1], (mdsdsc_a_t *)&dsc_shape));

size_t dimct = 0;
size_t original_count = (arr->arsize / arr->length);
size_t new_count = 1;
for (size_t i = 0; i < MAX_DIMS; ++i) {
if (shape[i] == 0) {
break;
}

++dimct;
new_count *= shape[i];
}

if (original_count != new_count) {
return TdiMISMATCH;
}

if (dimct <= arr->dimct) {
arr->dimct = dimct;
for (size_t i = 0; i < dimct; ++i) {
arr->m[i] = shape[i];
}
}
else {
// Need to resize the array descriptor to fit the new dims

array_coeff new_array = {
.length = arr->length,
.dtype = arr->dtype,
.class = arr->class,
.pointer = arr->pointer,
.scale = arr->scale,
.digits = arr->digits,
.aflags = arr->aflags,
.dimct = dimct,
.arsize = arr->arsize,
};

new_array.aflags.coeff = 1;
new_array.dimct = dimct;
new_array.a0 = new_array.pointer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it points at the same memory - good
Do you check if it is the right size for the dimensions ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! That's done up on line 77, I compare the original count to the product of the shape, which should be the new size

for (size_t i = 0; i < dimct; ++i) {
new_array.m[i] = shape[i];
}

mdsdsc_xd_t tmp_out_ptr = MDSDSC_XD_INITIALIZER;
MdsCopyDxXd((mdsdsc_t *)&new_array, &tmp_out_ptr);

// Use the new array, not the original one
MdsFree1Dx(out_ptr, NULL);
memcpy(out_ptr, &tmp_out_ptr, sizeof(mdsdsc_xd_t));
}

break;
}
case OPC_FLATTEN:
{
if (arr->dimct > 1)
Expand Down