Skip to content

Commit

Permalink
add utils (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihsin authored Dec 23, 2024
1 parent 2a97f2e commit 46b89e3
Show file tree
Hide file tree
Showing 6 changed files with 893 additions and 1 deletion.
780 changes: 780 additions & 0 deletions src/cpp/include/cytnx_core/lapack_wrapper.hpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ target_sources_local(cytnx_core
)


#add_subdirectory(storage)
add_subdirectory(utils_internal)
5 changes: 5 additions & 0 deletions src/cpp/src/utils_internal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_subdirectory(cpu)

if(USE_CUDA)
add_subdirectory(gpu)
endif()
6 changes: 6 additions & 0 deletions src/cpp/src/utils_internal/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target_sources_local(cytnx_core
PRIVATE

Complexmem_cpu.cpp
Complexmem_cpu.hpp
)
76 changes: 76 additions & 0 deletions src/cpp/src/utils_internal/cpu/Complexmem_cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "Complexmem_cpu.hpp"

#include <cytnx_core/lapack_wrapper.hpp>
#include <cytnx_core/errors/cytnx_error.hpp>

#ifdef UNI_OMP
#include <omp.h>
#endif

#ifdef UNI_MKL
#define __Cpt_dbl cytnx_complex128 *
#define __Cpt_flt cytnx_complex64 *
#else
#define __Cpt_dbl __complex__ double *
#define __Cpt_flt __complex__ float *
#endif

using namespace std;
namespace cytnx_core {

namespace utils_internal {

void Complexmem_cpu_cdtd(void *out, void *in, const cytnx_uint64 &Nelem, const bool get_real) {
cytnx_double *des = static_cast<cytnx_double *>(out);
cytnx_complex128 *src = static_cast<cytnx_complex128 *>(in);

if (get_real) {
#pragma omp parallel for schedule(dynamic)
for (cytnx_uint64 n = 0; n < Nelem; n++) {
des[n] = src[n].real();
}
} else {
#pragma omp parallel for schedule(dynamic)
for (cytnx_uint64 n = 0; n < Nelem; n++) {
des[n] = src[n].imag();
}
}
}

void Complexmem_cpu_cftf(void *out, void *in, const cytnx_uint64 &Nelem, const bool get_real) {
cytnx_float *des = static_cast<cytnx_float *>(out);
cytnx_complex64 *src = static_cast<cytnx_complex64 *>(in);

if (get_real) {
#pragma omp parallel for schedule(dynamic)
for (cytnx_uint64 n = 0; n < Nelem; n++) {
des[n] = src[n].real();
}
} else {
#pragma omp parallel for schedule(dynamic)
for (cytnx_uint64 n = 0; n < Nelem; n++) {
des[n] = src[n].imag();
}
}
}

void ComplexMatrix_from_real_cd(void *out, void *in, const cytnx_uint64 &m,
const cytnx_uint64 &n, const bool real_part) {
if (real_part)
LAPACKE_zlacp2(LAPACK_ROW_MAJOR, 'A', m, n, (double *)in, n, (__Cpt_dbl)out, n);
else
LAPACKE_zlacp2(LAPACK_ROW_MAJOR, 'A', m, n, (double *)in, n,
(__Cpt_dbl)(&((cytnx_double *)in)[1]), n);
}
void ComplexMatrix_from_real_cf(void *out, void *in, const cytnx_uint64 &m,
const cytnx_uint64 &n, const bool real_part) {
if (real_part)
LAPACKE_clacp2(LAPACK_ROW_MAJOR, 'A', m, n, (float *)in, n, (__Cpt_flt)out, n);
else
LAPACKE_clacp2(LAPACK_ROW_MAJOR, 'A', m, n, (float *)in, n,
(__Cpt_flt)(&((cytnx_float *)in)[1]), n);
}

} // namespace utils_internal

} // namespace cytnx_core
25 changes: 25 additions & 0 deletions src/cpp/src/utils_internal/cpu/Complexmem_cpu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CYTNX_BACKEND_UTILS_INTERNAL_CPU_COMPLEXMEM_CPU_H_
#define CYTNX_BACKEND_UTILS_INTERNAL_CPU_COMPLEXMEM_CPU_H_

#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#include <climits>
#include <cytnx_core/Type.hpp>

namespace cytnx_core {
namespace utils_internal {

void Complexmem_cpu_cdtd(void *out, void *in, const cytnx_uint64 &Nelem, const bool get_real);
void Complexmem_cpu_cftf(void *out, void *in, const cytnx_uint64 &Nelem, const bool get_real);

void ComplexMatrix_from_real_cd(void *out, void *in, const cytnx_uint64 &m,
const cytnx_uint64 &n, const bool real_part);

void ComplexMatrix_from_real_cf(void *out, void *in, const cytnx_uint64 &m,
const cytnx_uint64 &n, const bool real_part);

} // namespace utils_internal
} // namespace cytnx_core

#endif // CYTNX_BACKEND_UTILS_INTERNAL_CPU_COMPLEXMEM_CPU_H_

0 comments on commit 46b89e3

Please sign in to comment.