Skip to content

Commit

Permalink
Advanced Indexing Part 1 -- Purely Integer Array Indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
killeent authored and soumith committed Jun 22, 2017
1 parent e391789 commit 9d30c3a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/TH/generic/THTensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,42 @@ void THTensor_(expand)(THTensor *r, THTensor *tensor, THLongStorage *sizes) {
THFree(expandedStrides);
}


void THTensor_(expandNd)(THTensor **rets, THTensor **ops, int count) {
for (int i = 0; i < count; ++i) {
THArgCheck(THTensor_(nDimension)(ops[i]) > 0, i, "can't expand empty tensor %d", i);
}

long *op_sizes[count];
long op_dims[count];

for (int i = 0; i < count; ++i) {
op_sizes[i] = ops[i]->size;
op_dims[i] = ops[i]->nDimension;
}

THLongStorage *sizes = THLongStorage_new();
char error_buffer[1024];
int ret = THLongStorage_inferSizeN(sizes,
count,
op_sizes,
op_dims,
error_buffer,
1024);

if(ret != 0) {
THLongStorage_free(sizes);
THError(error_buffer);
return;
}

for (int i = 0; i < count; ++i) {
THTensor_(expand)(rets[i], ops[i], sizes);
}

THLongStorage_free(sizes);
}

void THTensor_(set)(THTensor *self, THTensor *src)
{
if(self != src)
Expand Down
1 change: 1 addition & 0 deletions lib/TH/generic/THTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ TH_API THTensor *THTensor_(newView)(THTensor *tensor, THLongStorage *size);
TH_API THTensor *THTensor_(newExpand)(THTensor *tensor, THLongStorage *size);

TH_API void THTensor_(expand)(THTensor *r, THTensor *tensor, THLongStorage *size);
TH_API void THTensor_(expandNd)(THTensor **rets, THTensor **ops, int count);

TH_API void THTensor_(resize)(THTensor *tensor, THLongStorage *size, THLongStorage *stride);
TH_API void THTensor_(resizeAs)(THTensor *tensor, THTensor *src);
Expand Down

4 comments on commit 9d30c3a

@zhangwlgq
Copy link

Choose a reason for hiding this comment

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

  • long *op_sizes[count];
  • long op_dims[count];

may cause build error

@soumith
Copy link
Member

Choose a reason for hiding this comment

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

whats the error message you are seeing?

@shijie-nv
Copy link

Choose a reason for hiding this comment

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

Visual studio does not support VLAs (variable length arrays).
So the "long *op_sizes[count];" will cause build error on Windows.

@shijie-nv
Copy link

Choose a reason for hiding this comment

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

d:\projects_git\torch\pkg\torch\lib\th\generic/THTensor.c(324) : error C2057: ex
pected constant expression
d:\projects_git\torch\pkg\torch\lib\th\generic/THTensor.c(324) : error C2466: ca
nnot allocate an array of constant size 0

Please sign in to comment.