   magma_free_pinned(ipiv);
   THCTensor_(freeCopyTo)(state, input, ra_);
 #else
-  THArgCheck(a->nDimension == 2, 2, "A should be 2 dimensional");
-  THArgCheck(a->size[0] == a->size[1], 2, "A should be square");
-
   int n = a->size[0];
 
   // input
-  THCTensor *input = THCTensor_(newColumnMajor)(state, ra_, a);
-  // output
-  THCTensor *output = THCTensor_(newColumnMajor)(state, ra_, a);
-
-  size_t matrices_size = sizeof(real*);
-
-  real **matrices1 = (real **)THAlloc(matrices_size);
-  const real **matrices1_const = (const real **)THAlloc(matrices_size);
-  real **matrices2 = (real **)THAlloc(matrices_size);
-  matrices1[0] = THCTensor_(data)(state, input);
-  matrices1_const[0] = THCTensor_(data)(state, input);
-  matrices2[0] = THCTensor_(data)(state, output);
+  THCTensor *input = THCTensor_(newColumnMajor)(state, a, a);
+  THCTensor_(resizeNd)(state, ra_, 2, input->size, input->stride);
+
+  real *matrices1[1] = { THCTensor_(data)(state, input) };
+  real *matrices2[1] = { THCTensor_(data)(state, ra_) };
 
   // Copy pointers to device.
   real **d_matrices1, **d_matrices2;
-  const real **d_matrices1_const;
-  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices1, matrices_size));
-  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices1_const, matrices_size));
-  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices2, matrices_size));
-
-  THCudaCheck(cudaMemcpyAsync(d_matrices1, matrices1, matrices_size,
+  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices1, sizeof(real*)));
+  THCudaCheck(THCudaMalloc(state, (void**)&d_matrices2, sizeof(real*)));
+
+  THCudaCheck(cudaMemcpyAsync(d_matrices1, matrices1, sizeof(real*),
                               cudaMemcpyHostToDevice, THCState_getCurrentStream(state)));
-  THCudaCheck(cudaMemcpyAsync(d_matrices1_const, matrices1_const, matrices_size,
-                              cudaMemcpyHostToDevice, THCState_getCurrentStream(state)));
-  THCudaCheck(cudaMemcpyAsync(d_matrices2, matrices2, matrices_size,
+  THCudaCheck(cudaMemcpyAsync(d_matrices2, matrices2, sizeof(real*),
                               cudaMemcpyHostToDevice, THCState_getCurrentStream(state)));
   int info;
   int *info_gpu;

  THCudaCheck(THCudaFree(state, info_gpu));

  THCudaCheck(THCudaFree(state, d_matrices1));
  THCudaCheck(THCudaFree(state, d_matrices1_const));
  THCudaCheck(THCudaFree(state, d_matrices2));

  THCTensor_(freeCopyTo)(state, output, input);
#endif
}



THC_API void THCTensor_(getri)(THCState *state, THCTensor *ra_, THCTensor *a)
{
  THArgCheck(a->nDimension == 2, 2, "A should be 2 dimensional");
  THArgCheck(a->size[0] == a->size[1], 2, "A should be square");

#ifdef USE_MAGMA
  int info;
  int n = a->size[0];
  int lwork = n * magma_get_sgetri_nb(n);
