Skip to content

Commit

Permalink
do contiguous automatically before (i)Add/(i)Sub
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffry1829 committed Jul 26, 2023
1 parent c9717a5 commit 5b730a8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/linalg/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,20 @@ namespace cytnx {
Rt._impl->invmapper(), 0);
} else {
#ifdef UNI_GPU
cytnx_error_msg(true,
"[Add][on GPU/CUDA] error two tensors must be contiguous. Call "
"Contiguous_() or Contiguous() first%s",
"\n");
// cytnx_error_msg(true,
// "[Add][on GPU/CUDA] error two tensors must be contiguous. Call "
// "Contiguous_() or Contiguous() first%s",
// "\n");
cytnx_warning_msg(
true,
"[Add][on GPU/CUDA] error two tensors must be contiguous. Call Contiguous_() or "
"Contiguous() first. Automatically did it.%s",
"\n");
Tensor _Tl = Lt.contiguous(), _Tr = Rt.contiguous();
checkCudaErrors(cudaSetDevice(Rt.device()));
linalg_internal::lii.cuAri_ii[Lt.dtype()][Rt.dtype()](
out._impl->storage()._impl, _Tl._impl->storage()._impl, _Tr._impl->storage()._impl,
out._impl->storage()._impl->size(), {}, {}, {}, 0);
#else
cytnx_error_msg(true, "[Add] fatal error, the tensor is on GPU without CUDA support.%s",
"\n");
Expand Down
18 changes: 14 additions & 4 deletions src/linalg/Sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ namespace cytnx {
Rt._impl->invmapper(), 2);
} else {
#ifdef UNI_GPU
cytnx_error_msg(true,
"[Sub][on GPU/CUDA] error two tensors must be contiguous. Call "
"Contiguous_() or Contiguous() first%s",
"\n");
// cytnx_error_msg(true,
// "[Sub][on GPU/CUDA] error two tensors must be contiguous. Call "
// "Contiguous_() or Contiguous() first%s",
// "\n");
cytnx_warning_msg(
true,
"[Sub][on GPU/CUDA] error two tensors must be contiguous. Call Contiguous_() or "
"Contiguous() first. Automatically did it.%s",
"\n");
Tensor _Tl = Lt.contiguous(), _Tr = Rt.contiguous();
checkCudaErrors(cudaSetDevice(Rt.device()));
cytnx::linalg_internal::lii.cuAri_ii[Lt.dtype()][Rt.dtype()](
out._impl->storage()._impl, _Tl._impl->storage()._impl, _Tr._impl->storage()._impl,
out._impl->storage()._impl->size(), {}, {}, {}, 2);
#else
cytnx_error_msg(true, "[Sub] fatal error, the tensor is on GPU without CUDA support.%s",
"\n");
Expand Down
28 changes: 24 additions & 4 deletions src/linalg/iSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,30 @@ namespace cytnx {
Rt._impl->invmapper(), 2);
} else {
#ifdef UNI_GPU
cytnx_error_msg(true,
"[iSub][on GPU/CUDA] error two tensors must be contiguous. Call "
"Contiguous_() or Contiguous() first%s",
"\n");
// cytnx_error_msg(true,
// "[iSub][on GPU/CUDA] error two tensors must be contiguous. Call "
// "Contiguous_() or Contiguous() first%s",
// "\n");
cytnx_warning_msg(
true,
"[iSub][on GPU/CUDA] error two tensors must be contiguous. Call Contiguous_() or "
"Contiguous() first. Automatically did it.%s",
"\n");

Lt.contiguous_();
R.contiguous_();
checkCudaErrors(cudaSetDevice(Rt.device()));
Tensor tmpo;
if (Lt.dtype() <= Rt.dtype())
tmpo = Lt;
else
tmpo = Lt.clone();
linalg_internal::lii.cuAri_ii[Lt.dtype()][Rt.dtype()](
tmpo._impl->storage()._impl, Lt._impl->storage()._impl, R._impl->storage()._impl,
Lt._impl->storage()._impl->size(), {}, {}, {}, 2);
// cytnx_error_msg(true, "[Developing] iAdd for GPU%s", "\n");

if (Lt.dtype() > Rt.dtype()) Lt = tmpo;

#else
cytnx_error_msg(true, "[Sub] fatal error, the tensor is on GPU without CUDA support.%s",
Expand Down

0 comments on commit 5b730a8

Please sign in to comment.