diff --git a/.github/workflows/autotest.yml b/.github/workflows/autotest.yml new file mode 100644 index 00000000..dcac43d6 --- /dev/null +++ b/.github/workflows/autotest.yml @@ -0,0 +1,130 @@ +# Based on Nick Naso's cpp.yml workflow, https://gist.github.com/NickNaso/0d478f1481686d5bcc868cac06620a60 + +on: + push: + pull_request: + release: + +jobs: + build_cpp: + name: ${{ matrix.config.name }} + runs-on: ${{ matrix.config.os }} + strategy: + fail-fast: false + matrix: + config: + - { + name: "Ubuntu_Latest_GCC", + os: ubuntu-latest, + artifact: "ubuntu_gcc.7z", + build_type: "Release", + cc: "gcc", + cxx: "g++", + archiver: "7z a", + } + - { + name: "Ubuntu_GCC_9", + os: ubuntu-latest, + artifact: "ubuntu_gcc9.7z", + build_type: "Release", + cc: "gcc", + cxx: "g++", + archiver: "7z a", + } + - { + name: "macOS Latest Clang", + os: macos-latest, + artifact: "macos_clang.7z", + build_type: "Release", + cc: "clang", + cxx: "clang++", + archiver: "7za a", + } + build_scalapack: [True, False] + steps: + - uses: actions/checkout@v2 + + - name: Print env + run: | + echo github.event.action: ${{ github.event.action }} + echo github.event_name: ${{ github.event_name }} + echo matrix.config.build_scalapack + echo ${{ matrix.config.build_scalapack }} + + - name: Install dependencies on ubuntu + if: startsWith(matrix.config.name, 'Ubuntu') + run: | + sudo apt-get update; + sudo apt install -y g++ gfortran git make libblas-dev liblapack-dev mpich + + - name: Install dependencies on macos + if: startsWith(matrix.config.os, 'macos') + run: | + brew install mpich + + - name: Configure Cyclops and Build Scalapack + if: matrix.config.build_scalapack + shell: bash + run: + ./configure CXXFLAGS="-O0" --build-scalapack + + - name: Configure Cyclops without Scalapack + if: ( ! matrix.config.build_scalapack ) + shell: bash + run: + ./configure CXXFLAGS="-O0" + + + - name: Build Cyclops + shell: bash + run: + make -j4 + + - name: Build Tests and Test Cyclops C++ + run: + make test + + - name: Test Cyclops C++ with 2 MPI processes + if: startsWith(matrix.config.name, 'Ubuntu') + shell: bash + run: + export OMP_NUM_THREADS=1; + export MPIR_CVAR_DEVICE_COLLECTIVES=none; + make test2 + + + - name: Build Python Install dependencies on ubuntu + if: startsWith(matrix.config.name, 'Ubuntu') + run: | + sudo apt install -y python3-dev virtualenv; + mkdir envs + + - name: Build Python Install dependencies on macos + if: startsWith(matrix.config.os, 'macos') + run: | + brew install virtualenv + + - name: Create Python virtual environment and install dependencies via pip + run: | + virtualenv -p python3 ./envs/py3env; + source ./envs/py3env/bin/activate; + pip install numpy cython setuptools + + - name: Build Python library + run: + source ./envs/py3env/bin/activate; + make python + + - name: Test Cyclops Python + run: + source ./envs/py3env/bin/activate; + make python_test + + - name: Test Cyclops Python with 2 MPI processes + if: startsWith(matrix.config.name, 'Ubuntu') + shell: bash + run: + source ./envs/py3env/bin/activate; + export OMP_NUM_THREADS=1; + export MPIR_CVAR_DEVICE_COLLECTIVES=none; + make python_test2 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 87b4b397..00000000 --- a/.travis.yml +++ /dev/null @@ -1,57 +0,0 @@ -sudo: false -os: linux -language: python -python: - - "2.7" - - "3.5" - - "3.6" -env: - - CTF_CXX=clang++ - - CTF_CXX=g++ - -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - gcc-5 - - g++-5 - - gfortran-5 - - libgfortran-5-dev - - libblas-dev - - liblapack-dev - - mpich - - libmpich-dev - - cmake -before_install: - - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]] || [[ "$TRAVIS_PYTHON_VERSION" == "2.6" ]] ; then - export PYTHONMAJORV=2; - else - export PYTHONMAJORV=3; - fi - - wget https://repo.continuum.io/miniconda/Miniconda${PYTHONMAJORV}-latest-Linux-x86_64.sh - - bash Miniconda${PYTHONMAJORV}-latest-Linux-x86_64.sh -b - - export PATH=$HOME/miniconda${PYTHONMAJORV}/bin:$PATH - - conda create -y -n mypy python=$TRAVIS_PYTHON_VERSION - - source activate mypy - - conda install -y cython - - conda install -y numpy nomkl blas=*=openblas -install: - - $CTF_CXX --version - - FC=gfortran-6 ./configure CXX="mpicxx -cxx=$CTF_CXX" --build-hptt --build-scalapack - - make -j2 - - make python -j2 -script: - - make test - - make test2 - - make python_test - - make python_test2 -after_failure: -notifications: - email: - recipients: - - solomon2@illinois.edu - on_success: change - on_failure: always - - diff --git a/Makefile b/Makefile index 020902a4..889af5c0 100644 --- a/Makefile +++ b/Makefile @@ -130,7 +130,7 @@ pip: $(BDIR)/setup.py $(BDIR)/lib_shared/libctf.so $(PYTHON_SRC_FILES) cd src_python; \ ln -sf $(BDIR)/setup.py setup.py; \ mkdir -p $(BDIR)/lib_python/ctf && cp ctf/__init__.py $(BDIR)/lib_python/ctf/; \ - pip install --force -b $(BDIR)/lib_python/ . --upgrade; \ + pip install --force . --upgrade; \ rm setup.py; \ cd ..; diff --git a/README.md b/README.md index 3579ec76..deda5d34 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ ## Cyclops Tensor Framework (CTF) -[](https://travis-ci.org/cyclops-community/ctf) Cyclops is a parallel (distributed-memory) numerical library for multidimensional arrays (tensors) in C++ and Python. @@ -27,6 +26,12 @@ First, its necessary to run the configure script, which can be set to the approp ``` then execute ./configure with the appropriate options. Successful execution of this script, will generate a `config.mk` file and a `setup.py` file, needed for C++ and Python builds, respectively, as well as a how-did-i-configure file with info on how the build was configured. You may modify the `config.mk` and `setup.py` files thereafter, subsequent executions of configure will prompt to overwrite these files. +Note: there is a (now-fixed) [bug](https://github.com/pmodels/mpich/pull/6543) in recent versions of MPICH that causes a segmentation fault in CTF when executing with 2 or more processors. +The bug can be remedied without rebuilding CTF by setting an environment variable as follows, +```sh +export MPIR_CVAR_DEVICE_COLLECTIVES=none +``` + ### Dependencies and Supplemental Packages The strict library dependencies of Cyclops are MPI and BLAS libraries. diff --git a/bench/model_trainer.cxx b/bench/model_trainer.cxx index a15a2777..e6019d15 100644 --- a/bench/model_trainer.cxx +++ b/bench/model_trainer.cxx @@ -23,6 +23,7 @@ void train_off_vec_mat(int64_t n, int64_t m, World & dw, bool sp_A, bool sp_B, b void train_ttm(int64_t sz, int64_t r, World & dw){ Timer TTM("TTM"); TTM.start(); + srand48(dw.rank); for (int order=2; order<7; order++){ int64_t n = 1; while (std::pow(n,order) < sz){ @@ -87,6 +88,7 @@ void train_ttm(int64_t sz, int64_t r, World & dw){ void train_sparse_mttkrp(int64_t sz, int64_t R, World & dw){ Timer sMTTKRP("sMTTKRP"); sMTTKRP.start(); + srand48(dw.rank); for (double sp = .1; sp>.000001; sp*=.25){ int64_t n = (int64_t)cbrt(sz/sp); int64_t lens[3] = {n, n, n}; @@ -223,6 +225,7 @@ void train_sps_vec_mat(int64_t n, int64_t m, World & dw, bool sp_A, bool sp_B, b void train_ccsd(int64_t n, int64_t m, World & dw){ Timer ccsd_t("CCSD"); ccsd_t.start(); + srand48(dw.rank); int nv = sqrt(n); int no = sqrt(m); Integrals V(no, nv, dw); @@ -271,18 +274,15 @@ void train_world(double dtime, World & dw, double step_size){ } train_sparse_mttkrp(n*m/8, m, dw); train_dns_vec_mat(n, m, dw); - train_sps_vec_mat(n-2, m, dw, 0, 0, 0); - train_sps_vec_mat(n-4, m-2, dw, 1, 0, 0); - train_sps_vec_mat(n-1, m-4, dw, 1, 1, 0); - train_sps_vec_mat(n-2, m-3, dw, 1, 1, 1); - train_off_vec_mat(n+7, m-4, dw, 0, 0, 0); - train_off_vec_mat(n-2, m+6, dw, 1, 0, 0); - train_off_vec_mat(n-5, m+2, dw, 1, 1, 0); - train_off_vec_mat(n-3, m-1, dw, 1, 1, 1); - train_ccsd(n/2, m/2, dw); + train_sps_vec_mat(n, m, dw, 0, 0, 0); + train_sps_vec_mat(n, m, dw, 0, 0, 1); + train_off_vec_mat(n, m, dw, 0, 0, 0); + train_off_vec_mat(n, m, dw, 1, 0, 0); + train_off_vec_mat(n, m, dw, 1, 1, 0); + train_off_vec_mat(n, m, dw, 1, 1, 1); + train_ccsd(n, m, dw); train_sparse_mp3(n,m,dw); niter++; - // m *= 1.9; m *= step_size; n += 2; ctime = MPI_Wtime() - t_st; @@ -310,7 +310,8 @@ void frize(std::set & ps, int p){ } } -void train_all(double time, bool write_coeff, bool dump_data, std::string coeff_file, std::string data_dir){ +void train_all(double time, bool write_coeff, bool dump_data, std::string coeff_file, std::string data_dir, + int num_iterations, double time_jump, int verbose){ World dw(MPI_COMM_WORLD); int np = dw.np; int rank; @@ -333,13 +334,6 @@ void train_all(double time, bool write_coeff, bool dump_data, std::string coeff_ MPI_Comm_split(dw.comm, color, key, &cm); World w(cm); - // number of iterations for training - int num_iterations = 5; - - // control how much dtime should be increased upon each iteration - // dtime = dtime * time_dump at the end of each iteration - double time_jump = 1.5; - double dtime = (time / (1- 1/time_jump)) / pow(time_jump, num_iterations - 1.0); for (int i=0; i 1) verbose = 0; + } else verbose = 0; + + // number of iterations for training + if (getCmdOption(input_str, input_str+in_num, "-niter")){ + num_iterations = atoi(getCmdOption(input_str, input_str+in_num, "-niter")); + } else num_iterations = 5; + + // control how much dtime should be increased upon each iteration + // dtime = dtime * time_jump at the end of each iteration + if (getCmdOption(input_str, input_str+in_num, "-time_jump")){ + time_jump = atof(getCmdOption(input_str, input_str+in_num, "-time_jump")); + } else time_jump = 1.5; // Boolean expression that are used to pass command line argument to function train_all bool write_coeff = false; @@ -460,7 +469,7 @@ int main(int argc, char ** argv){ printf("Executing a wide set of contractions to train model with time budget of %lf sec\n", time); if (write_coeff) printf("At the end of execution write new coefficients will be written to model file %s\n",file_path); } - train_all(time, write_coeff, dump_data, coeff_file, data_dir_str); + train_all(time, write_coeff, dump_data, coeff_file, data_dir_str, num_iterations, time_jump, verbose); } diff --git a/configure b/configure index 95da8250..aa8c4a32 100755 --- a/configure +++ b/configure @@ -1357,8 +1357,7 @@ fi cat > $BUILDDIR/setup.py <sym[0] != NS){ Matrix A(this->nrow, this->ncol, *this->wrld); A["ij"] = this->operator[]("ij"); - return A.cholesky(L, lower); + A.cholesky(L, lower); + t_cholesky.stop(); + return; } int info; int m = this->nrow; @@ -650,12 +652,16 @@ namespace CTF { if (this->sym[0] != NS){ Matrix B(this->nrow, this->ncol, *this->wrld); B["ij"] = this->operator[]("ij"); - return B.solve_tri(L, X, lower, from_left, transp_L); + B.solve_tri(L, X, lower, from_left, transp_L); + t_solve_tri.stop(); + return; } if (L.sym[0] != NS){ Matrix LF(this->nrow, this->ncol, *this->wrld); LF["ij"] = L["ij"]; - return this->solve_tri(L, X, lower, from_left, transp_L); + this->solve_tri(L, X, lower, from_left, transp_L); + t_solve_tri.stop(); + return; } int m = this->nrow; @@ -748,7 +754,9 @@ namespace CTF { if (M.sym[0] != NS || M.edge_map[0].calc_phase() != M.edge_map[1].calc_phase()){ Matrix MM(M.nrow, M.ncol, "ij", proc_grid_2d["ij"], virt_grid_1d["j"], 0, *M.wrld); MM["ij"] = M.operator[]("ij"); - return this->solve_spd(MM, X); + this->solve_spd(MM, X); + t_solve_spd.stop(); + return; } int info; @@ -929,7 +937,9 @@ namespace CTF { if (this->sym[0] != NS){ Matrix A(this->nrow, this->ncol, *this->wrld); A["ij"] = this->operator[]("ij"); - return A.qr(Q,R); + A.qr(Q,R); + t_qr.stop(); + return; } int info; @@ -998,7 +1008,9 @@ namespace CTF { if (this->sym[0] != NS){ Matrix A(this->nrow, this->ncol, *this->wrld); A["ij"] = this->operator[]("ij"); - return A.svd(U,S,VT,rank,threshold); + A.svd(U,S,VT,rank,threshold); + t_svd.stop(); + return; } int info; @@ -1143,7 +1155,9 @@ namespace CTF { if (this->sym[0] != NS){ Matrix A(this->nrow, this->ncol, *this->wrld); A["ij"] = this->operator[]("ij"); - return A.svd_rand(U,S,VT,rank,iter,oversamp,U_guess); + A.svd_rand(U,S,VT,rank,iter,oversamp,U_guess); + t_svd.stop(); + return; } int max_rank = std::min(std::min(nrow,ncol), (int64_t)rank+oversamp); IASSERT(rank+oversamp <= std::min(nrow,ncol) || U_guess==NULL); @@ -1184,7 +1198,9 @@ namespace CTF { if (this->sym[0] != NS){ Matrix A(this->nrow, this->ncol, *this->wrld); A["ij"] = this->operator[]("ij"); - return A.eigh(U,D); + A.eigh(U,D); + t_eigh.stop(); + return; } int info; diff --git a/src/interface/set.h b/src/interface/set.h index 956603ad..c9df4fa4 100644 --- a/src/interface/set.h +++ b/src/interface/set.h @@ -663,6 +663,30 @@ namespace CTF { std::sort((dtypePair*)pairs,((dtypePair*)pairs)+n); } + void to_s_of_a(int64_t n, char const * pairs, int64_t * keys, char * vals) const { + Pair const * dpairs = (Pair const *)pairs; + dtype * dvals = (dtype*)vals; +#ifdef _OPENMP + #pragma omp parallel for +#endif + for (int64_t i=0; i * dpairs = (Pair*)pairs; + dtype const * dvals = (dtype const*)vals; +#ifdef _OPENMP + #pragma omp parallel for +#endif + for (int64_t i=0; ito_s_of_a(*npair, cpairs, *global_idx, (char*)*data); if (cpairs != NULL) sr->pair_dealloc(cpairs); } @@ -425,13 +417,14 @@ namespace CTF { int64_t i; char * cpairs = sr->pair_alloc(npair); Pair< dtype > * pairs =(Pair< dtype >*)cpairs; -#ifdef _OPENMP - #pragma omp parallel for -#endif - for (i=0; ito_a_of_s(npair, global_idx, (char*)data, (char*)pairs); +//#ifdef _OPENMP +// #pragma omp parallel for +//#endif +// for (i=0; ipair_alloc(npair); Pair< dtype > * pairs =(Pair< dtype >*)cpairs; -#ifdef _OPENMP - #pragma omp parallel for -#endif - for (i=0; ito_a_of_s(npair, global_idx, (char*)data, (char*)pairs); +//#ifdef _OPENMP +// #pragma omp parallel for +//#endif +// for (i=0; ipair_alloc(npair); CTF_int::PairIterator pairs = CTF_int::PairIterator(sr, cpairs); for (i=0; ipair_alloc(npair); Pair< dtype > * pairs =(Pair< dtype >*)cpairs; - for (i=0; ito_a_of_s(npair, global_idx, (char*)data, (char*)pairs); + //for (i=0; i #endif diff --git a/src/shared/int_timer.cxx b/src/shared/int_timer.cxx index 9b8c7a8f..e1316aac 100644 --- a/src/shared/int_timer.cxx +++ b/src/shared/int_timer.cxx @@ -137,10 +137,13 @@ namespace CTF{ if (index == (int)function_timers->size()) { function_timers->push_back(Function_timer(name, MPI_Wtime(), excl_time)); } - timer_name = name; + strcpy(this->timer_name,name); exited = 0; } #endif + #ifdef CRITTER + strcpy(this->timer_name,name);// Save name for subsequent critter invocation + #endif } void Timer::start(){ @@ -153,6 +156,9 @@ namespace CTF{ } } #endif + #ifdef CRITTER + critter::start_timer(this->timer_name); + #endif } void Timer::stop(){ @@ -174,11 +180,15 @@ namespace CTF{ } } #endif + #ifdef CRITTER + critter::stop_timer(this->timer_name); + #endif } Timer::~Timer(){ } void print_timers(char const * name){ + #ifdef PROFILE int rank, np, i, j, len_symbols, nrecv_symbols; int is_fin = 0; @@ -297,6 +307,7 @@ namespace CTF{ fclose(output); } */ + #endif } void Timer::exit(){ diff --git a/src/shared/int_timer.h b/src/shared/int_timer.h index dfc74542..bde6608b 100644 --- a/src/shared/int_timer.h +++ b/src/shared/int_timer.h @@ -43,6 +43,15 @@ namespace CTF { else CTF::set_context((MPI_Comm)ARG); #endif +#ifdef CRITTER +#define TAU_FSTART CRITTER_START +#define TAU_FSTOP CRITTER_STOP +#endif + +#if !defined(PROFILE) && !defined(CRITTER) +#define TAU_FSTART(ARG) +#define TAU_FSTOP(ARG) +#endif #endif diff --git a/src/shared/model.h b/src/shared/model.h index e9b3a7d8..2ff60885 100644 --- a/src/shared/model.h +++ b/src/shared/model.h @@ -2,7 +2,8 @@ #define __MODEL_H__ #ifdef CRITTER -#include "critter.h" +#include "critter_mpi.h" +#include "critter_symbol.h" #else #include #endif diff --git a/src/shared/offload.cu b/src/shared/offload.cu index 09a04e4b..1b6a97e6 100644 --- a/src/shared/offload.cu +++ b/src/shared/offload.cu @@ -18,6 +18,10 @@ namespace CTF_int{ #ifndef PROFILE + #ifndef CRITTER + #define TAU_FSTART(ARG) + #define TAU_FSTOP(ARG) + #endif #define TAU_PROFILE(NAME,ARG,USER) #define TAU_PROFILE_TIMER(ARG1, ARG2, ARG3, ARG4) #define TAU_PROFILER_CREATE(ARG1, ARG2, ARG3, ARG4) @@ -25,8 +29,6 @@ namespace CTF_int{ #define TAU_PROFILE_START(ARG) #define TAU_PROFILE_SET_NODE(ARG) #define TAU_PROFILE_SET_CONTEXT(ARG) - #define TAU_FSTART(ARG) - #define TAU_FSTOP(ARG) #endif #define ABORT \ diff --git a/src/shared/pmpi.h b/src/shared/pmpi.h index b55f4b4c..6bd826b9 100644 --- a/src/shared/pmpi.h +++ b/src/shared/pmpi.h @@ -2,7 +2,8 @@ #define __PMPI_H__ #ifdef CRITTER -#include "critter.h" +#include "critter_mpi.h" +#include "critter_symbol.h" #else #include #endif diff --git a/src/symmetry/symmetrization.cxx b/src/symmetry/symmetrization.cxx index 99a5a6f5..6a869135 100644 --- a/src/symmetry/symmetrization.cxx +++ b/src/symmetry/symmetrization.cxx @@ -76,14 +76,14 @@ namespace CTF_int { #ifdef PROF_SYM if (sym_tsr->wrld->rank == 0) VPRINTF(2,"Desymmetrizing %s\n", sym_tsr->name); + CTF::Timer t_pf(spf); + t_pf.start(); if (sym_tsr->profile) { char spf[80]; strcpy(spf,"desymmetrize_"); strcat(spf,sym_tsr->name); - CTF::Timer t_pf(spf); if (sym_tsr->wrld->rank == 0) VPRINTF(2,"Desymmetrizing %s\n", sym_tsr->name); - t_pf.start(); } #endif @@ -229,9 +229,8 @@ namespace CTF_int { char spf[80]; strcpy(spf,"desymmetrize_"); strcat(spf,sym_tsr->name); - CTF::Timer t_pf(spf); - t_pf.stop(); } + t_pf.stop(); #endif TAU_FSTOP(desymmetrize); @@ -247,14 +246,14 @@ namespace CTF_int { TAU_FSTART(symmetrize); #ifdef PROF_SYM + CTF::Timer t_pf(spf); + t_pf.start(); if (sym_tsr->profile) { char spf[80]; strcpy(spf,"symmetrize_"); strcat(spf,sym_tsr->name); - CTF::Timer t_pf(spf); if (sym_tsr->wrld->rank == 0) VPRINTF(2,"Symmetrizing %s\n", sym_tsr->name); - t_pf.start(); } #endif @@ -391,9 +390,8 @@ namespace CTF_int { char spf[80]; strcpy(spf,"symmetrize_"); strcat(spf,sym_tsr->name); - CTF::Timer t_pf(spf); - t_pf.stop(); } + t_pf.stop(); #endif diff --git a/src/tensor/algstrct.cxx b/src/tensor/algstrct.cxx index 855615e2..ce9ee570 100644 --- a/src/tensor/algstrct.cxx +++ b/src/tensor/algstrct.cxx @@ -262,6 +262,48 @@ namespace CTF_int { } + void algstrct::to_s_of_a(int64_t n, char const * pairs, int64_t * keys, char * vals) const { + ConstPairIterator rA(this, pairs); +#ifdef _OPENMP + #pragma omp parallel for +#endif + for (int64_t i=0; isort(n, ptr); } - void ConstPairIterator::permute(int64_t n, int order, int64_t const * old_lens, int64_t const * new_lda, PairIterator wA){ - ConstPairIterator rA = * this; -#ifdef USE_OMP - #pragma omp parallel for -#endif - for (int64_t i=0; iel_size*(*num_pair)); + sr->to_s_of_a(*num_pair, pairs, *inds, *vals); + } + int64_t tensor::get_tot_size(bool packed=false){ if (!packed){ int64_t tsize = 1; @@ -2964,14 +2971,12 @@ namespace CTF_int { } #endif #ifdef PROF_REDIST + Timer t_pf(spf); + t_pf.start(); if (this->profile) { char spf[80]; strcpy(spf,"redistribute_"); strcat(spf,this->name); - if (wrld->cdt.rank == 0){ - Timer t_pf(spf); - t_pf.start(); - } } #endif #if VERIFY_REMAP @@ -3052,11 +3057,8 @@ namespace CTF_int { char spf[80]; strcpy(spf,"redistribute_"); strcat(spf,this->name); - if (wrld->cdt.rank == 0){ - Timer t_pf(spf); - t_pf.stop(); - } } + t_pf.stop(); #endif #if VERBOSE >=1 if (wrld->cdt.rank == 0){ @@ -3295,6 +3297,7 @@ namespace CTF_int { TAU_FSTART(zero_out_padding); if (this->has_zero_edge_len || is_sparse){ + TAU_FSTOP(zero_out_padding); return SUCCESS; } this->unfold(); diff --git a/src/tensor/untyped_tensor.h b/src/tensor/untyped_tensor.h index 6a24b6a5..915afdc4 100644 --- a/src/tensor/untyped_tensor.h +++ b/src/tensor/untyped_tensor.h @@ -508,6 +508,18 @@ namespace CTF_int { */ char * read_all_pairs(int64_t * num_pair, bool unpack, bool nonzero_only=false) const; + + /** + * \brief read all pairs with each processor (packed) + * \param[out] num_pair number of values read + * \param[in] unpack whether to read all or unique pairs up to symmetry + * \param[out] inds array of indices of nonzeros or all values + * \param[out] vals array of nonzeros or all values + * \param[in] nonzero_only whether to read only nonzeros + */ + void read_all_pairs(int64_t * num_pair, bool unpack, int64_t ** inds, char ** vals, bool nonzero_only=false) const; + + /** * \brief accumulates out a slice (block) of this tensor = B * B[offsets,ends)=beta*B[offsets,ends) + alpha*A[offsets_A,ends_A) diff --git a/src_python/ctf/__init__.py b/src_python/ctf/__init__.py index 0fe05ff6..f5c4337a 100644 --- a/src_python/ctf/__init__.py +++ b/src_python/ctf/__init__.py @@ -1,4 +1,3 @@ -from ctf import random from ctf.world import * from ctf.tensor import * from ctf.term import * @@ -7,4 +6,4 @@ from ctf.partition import * from ctf.multilinear import * from ctf.linalg import * - +from ctf import random diff --git a/src_python/ctf/chelper.pxd b/src_python/ctf/chelper.pxd index beba8bec..0bc865fc 100644 --- a/src_python/ctf/chelper.pxd +++ b/src_python/ctf/chelper.pxd @@ -1,7 +1,7 @@ from libc.stdint cimport int64_t cdef char* char_arr_py_to_c(a) - cdef int64_t* int64_t_arr_py_to_c(a) - cdef int* int_arr_py_to_c(a) +cdef _cast_carray_as_python(n, char * cdata, dtype) + diff --git a/src_python/ctf/chelper.pyx b/src_python/ctf/chelper.pyx index 4431486f..93b4d5c8 100644 --- a/src_python/ctf/chelper.pyx +++ b/src_python/ctf/chelper.pyx @@ -1,5 +1,11 @@ import numpy as np from libc.stdlib cimport malloc, free +from libc.stdint cimport int64_t +from libcpp cimport bool + +ctypedef double complex complex128_t +ctypedef float complex complex64_t +ctypedef long long iint64_t cdef char* char_arr_py_to_c(a): cdef char * ca @@ -32,4 +38,40 @@ cdef int* int_arr_py_to_c(a): ca[i] = a[i] return ca +cdef _cast_carray_as_python(n, char * cdata, dtype): + if dtype == np.float64: + return np.asarray(cdata) + elif dtype == np.float32: + return np.asarray(cdata) + elif dtype == np.complex64: + return np.asarray(cdata) + elif dtype == np.complex128: + return np.asarray(cdata) + elif dtype == np.int64: + return np.asarray(cdata) + elif dtype == np.int32: + return np.asarray(cdata) + elif dtype == np.bool_: + return np.asarray(cdata) + + else: + print(dtype) + raise ValueError('CTF PYTHON ERROR: bad dtype') + return np.ndarray() + +#WARNING: copy versions below inadequate for by-reference usage of above to write into C++ arrays +#cdef _cast_complex128_array_as_python(n, complex128_t * cdata): +# cdef complex128_t[:] cview = cdata +# data = np.empty(n, dtype=np.cdouble) +# data[:] = cview[:] +# free(cdata) +# return data +# +# +#cdef _cast_int64_array_as_python(n, int64_t * cdata): +# cdef int64_t[:] cview = cdata +# data = np.empty(n, dtype=np.int64) +# data[:] = cview[:] +# return data + diff --git a/src_python/ctf/helper.pyx b/src_python/ctf/helper.pyx index 062cd664..98de5c93 100644 --- a/src_python/ctf/helper.pyx +++ b/src_python/ctf/helper.pyx @@ -16,7 +16,7 @@ def _ord_comp(o1,o2): type_index = {} -type_index[np.bool] = 1 +type_index[np.bool_] = 1 type_index[np.int32] = 2 type_index[np.int64] = 3 type_index[np.float32] = 4 diff --git a/src_python/ctf/linalg.pyx b/src_python/ctf/linalg.pyx index edb14ab1..c52b05a3 100644 --- a/src_python/ctf/linalg.pyx +++ b/src_python/ctf/linalg.pyx @@ -1,9 +1,9 @@ from libcpp cimport bool -from tensor cimport ctensor, tensor +from ctf.tensor cimport ctensor, tensor -from helper import * -from chelper cimport * +from ctf.helper import * +from ctf.chelper cimport * from libc.stdlib cimport malloc, free import ctf.profile diff --git a/src_python/ctf/multilinear.pyx b/src_python/ctf/multilinear.pyx index a7811191..2b05094d 100644 --- a/src_python/ctf/multilinear.pyx +++ b/src_python/ctf/multilinear.pyx @@ -1,8 +1,8 @@ import numpy as np -from tensor cimport Tensor, tensor, ctensor +from ctf.tensor cimport Tensor, tensor, ctensor from libcpp cimport bool from libc.stdlib cimport malloc, free -from profile import timer +from ctf.profile import timer cdef extern from "ctf.hpp" namespace "CTF": cdef void TTTP_ "CTF::TTTP"[dtype](Tensor[dtype] * T, int num_ops, int * modes, Tensor[dtype] ** mat_list, bool aux_mode_first) diff --git a/src_python/ctf/partition.pyx b/src_python/ctf/partition.pyx index d126b36f..3594392a 100644 --- a/src_python/ctf/partition.pyx +++ b/src_python/ctf/partition.pyx @@ -1,5 +1,5 @@ from libc.stdlib cimport malloc, free -from chelper cimport * +from ctf.chelper cimport * cdef class partition: """ diff --git a/src_python/ctf/tensor.pxd b/src_python/ctf/tensor.pxd index aa1c76e2..12c7ecbd 100644 --- a/src_python/ctf/tensor.pxd +++ b/src_python/ctf/tensor.pxd @@ -1,8 +1,8 @@ from libc.stdint cimport int64_t from libcpp cimport bool cimport numpy as cnp -from partition cimport Idx_Partition -from world cimport World +from ctf.partition cimport Idx_Partition +from ctf.world cimport World cdef extern from "ctf.hpp" namespace "CTF": cdef cppclass Term: @@ -61,6 +61,7 @@ cdef extern from "ctf.hpp" namespace "CTF_int": void reshape(ctensor * tsr, char * alpha, char * beta) void allread(int64_t * num_pair, char * data, bool unpack) char * read_all_pairs(int64_t * num_pair, bool unpack, bool nonzeros_only) + void read_all_pairs(int64_t * num_pair, bool unpack, int64_t ** inds, char ** vals, bool nonzero_only) const; void slice(int64_t *, int64_t *, char *, ctensor *, int64_t *, int64_t *, char *) int64_t get_tot_size(bool packed) void get_raw_data(char **, int64_t * size) diff --git a/src_python/ctf/tensor.pyx b/src_python/ctf/tensor.pyx index f2cdce60..01225bb0 100644 --- a/src_python/ctf/tensor.pyx +++ b/src_python/ctf/tensor.pyx @@ -24,6 +24,7 @@ from ctf.partition cimport idx_partition #from ctf.helper import ctf.helper._ord_comp, ctf.helper.type_index, ctf.helper._rev_array, ctf.helper._get_np_dtype, ctf.helper._get_num_str, ctf.helper._use_align_for_pair from ctf.chelper cimport * +#cdef extern from "numpy/arrayobject.h": #from ctf.tensor_aux import ctf.tensor_aux.astensor, ctf.tensor_aux.transpose, ctf.tensor_aux.power, ctf.tensor_aux.dot, ctf.tensor_aux.reshape, ctf.tensor_aux.zeros, ctf.tensor_aux.conj, ctf.tensor_aux._match_tensor_types, ctf.tensor_aux._div, ctf.tensor_aux._setgetitem_helper, ctf.tensor_aux.trace, ctf.tensor_aux.diagonal, ctf.tensor_aux.take, ctf.tensor_aux.ravel tensorctf.tensor_aux.dot #from ctf.term import itensor #from ctf.world import comm @@ -423,7 +424,7 @@ cdef class tensor: if dtype is float: dtype = np.float64 - if dtype == np.complex: + if dtype == complex: dtype = np.complex128 @@ -447,7 +448,7 @@ cdef class tensor: self.sym = np.asarray([0]*self.ndim) else: self.sym = np.asarray(sym) - if self.dtype == np.bool: + if self.dtype == np.bool_: self.itemsize = 1 else: self.itemsize = np.dtype(self.dtype).itemsize @@ -488,7 +489,7 @@ cdef class tensor: self.dt = new Tensor[complex64_t](self.ndim, sp, clens, csym, wrld[0], idx.encode(), prl.ip[0], blk.ip[0]) if self.dtype == np.complex128: self.dt = new Tensor[complex128_t](self.ndim, sp, clens, csym, wrld[0], idx.encode(), prl.ip[0], blk.ip[0]) - if self.dtype == np.bool: + if self.dtype == np.bool_: self.dt = new Tensor[bool](self.ndim, sp, clens, csym, wrld[0], idx.encode(), prl.ip[0], blk.ip[0]) if self.dtype == np.int64: self.dt = new Tensor[int64_t](self.ndim, sp, clens, csym, wrld[0], idx.encode(), prl.ip[0], blk.ip[0]) @@ -507,7 +508,7 @@ cdef class tensor: self.dt = new Tensor[complex64_t](self.ndim, sp, clens, csym) elif self.dtype == np.complex128: self.dt = new Tensor[complex128_t](self.ndim, sp, clens, csym) - elif self.dtype == np.bool: + elif self.dtype == np.bool_: self.dt = new Tensor[bool](self.ndim, sp, clens, csym) elif self.dtype == np.int64: self.dt = new Tensor[int64_t](self.ndim, sp, clens, csym) @@ -601,7 +602,7 @@ cdef class tensor: raise ValueError("Universal functions among tensors with different order, i.e. Fortran vs C are not currently supported") out_order = self.order out_dtype = ctf.helper._get_np_dtype([self.dtype, other.dtype]) - out_dims = np.zeros(np.maximum(self.ndim, other.ndim), dtype=np.int) + out_dims = np.zeros(np.maximum(self.ndim, other.ndim), dtype=int) out_sp = min(self.sp,other.sp) out_sym = [SYM.NS]*len(out_dims) ind_coll = ctf.helper._get_num_str(3*out_dims.size) @@ -735,7 +736,9 @@ cdef class tensor: [idx_A, idx_B, idx_C, out_tsr] = tsr._ufunc_interpret(otsr) out_tsr.i(idx_C) << tsr.i(idx_A) - out_tsr.i(idx_C) << -1*otsr.i(idx_B) + iotsr = otsr.i(idx_B) + iotsr.scale(-1) + out_tsr.i(idx_C) << iotsr return out_tsr def __isub__(self, other_in): @@ -747,9 +750,13 @@ cdef class tensor: raise ValueError('CTF PYTHON ERROR: invalid call to __isub__ (-=)') if self.dtype != other.dtype: [tsr, otsr] = ctf.tensor_aux._match_tensor_types(self,other) # solve the bug when np.float64 -= np.int64 - self.i(idx_C) << -1*otsr.i(idx_A) + iotsr = otsr.i(idx_A) + iotsr.scale(-1) + self.i(idx_C) << iotsr else: - self.i(idx_C) << -1*other.i(idx_A) + iotsr = other.i(idx_A) + iotsr.scale(-1) + self.i(idx_C) << iotsr return self def __truediv__(self, other): @@ -833,7 +840,7 @@ cdef class tensor: self.dt.true_divide[int16_t](self.dt) elif self.dtype == np.int8: self.dt.true_divide[int8_t](self.dt) - elif self.dtype == np.bool: + elif self.dtype == np.bool_: self.dt.true_divide[bool](self.dt) def __matmul__(self, other): @@ -1064,7 +1071,7 @@ cdef class tensor: dims_keep = tuple(dims_keep) if out.shape != dims_keep: raise ValueError('CTF PYTHON ERROR: output must match when keepdims = True') - B = tensor((1,), dtype=np.bool) + B = tensor((1,), dtype=np.bool_) index_A = ctf.helper._get_num_str(self.ndim) if self.dtype == np.float64: all_helper[double](self.dt, B.dt, index_A.encode(), "".encode()) @@ -1076,7 +1083,7 @@ cdef class tensor: all_helper[int16_t](self.dt, B.dt, index_A.encode(), "".encode()) elif self.dtype == np.int8: all_helper[int8_t](self.dt, B.dt, index_A.encode(), "".encode()) - elif self.dtype == np.bool: + elif self.dtype == np.bool_: all_helper[bool](self.dt, B.dt, index_A.encode(), "".encode()) if out is not None: if out.dtype != B.get_type(): @@ -1129,12 +1136,12 @@ cdef class tensor: index_temp = ctf.helper._rev_array(index_A) index_B = index_temp[0:axis] + index_temp[axis+1:len(dim)] index_B = ctf.helper._rev_array(index_B) - B = tensor(dim_ret, dtype=np.bool) + B = tensor(dim_ret, dtype=np.bool_) if self.dtype == np.float64: all_helper[double](self.dt, B.dt, index_A.encode(), index_B.encode()) elif self.dtype == np.int64: all_helper[int64_t](self.dt, B.dt, index_A.encode(), index_B.encode()) - elif self.dtype == np.bool: + elif self.dtype == np.bool_: all_helper[bool](self.dt, B.dt, index_A.encode(), index_B.encode()) elif self.dtype == np.int32: all_helper[int32_t](self.dt, B.dt, index_A.encode(), index_B.encode()) @@ -1182,7 +1189,7 @@ cdef class tensor: for i in range(len(dim_ret)): if dim_ret[i] is not out.shape[i]: raise ValueError('CTF PYTHON ERROR: output parameter dimensions mismatch') - B = tensor(dim_ret, dtype=np.bool) + B = tensor(dim_ret, dtype=np.bool_) index_A = ctf.helper._get_num_str(self.ndim) index_temp = ctf.helper._rev_array(index_A) index_B = "" @@ -1200,7 +1207,7 @@ cdef class tensor: all_helper[int16_t](self.dt, B.dt, index_A.encode(), index_B.encode()) elif self.dtype == np.int8: all_helper[int8_t](self.dt, B.dt, index_A.encode(), index_B.encode()) - elif self.dtype == np.bool: + elif self.dtype == np.bool_: all_helper[bool](self.dt, B.dt, index_A.encode(), index_B.encode()) if out is not None: if out.dtype is not B.get_type(): @@ -1617,7 +1624,7 @@ cdef class tensor: if dtype == float: dtype = np.float64 if str(dtype) == "": - dtype = np.bool + dtype = np.bool_ if str(dtype) == "": dtype = np.complex128 B = tensor(self.shape, sp=self.sp, dtype = dtype) @@ -1628,10 +1635,10 @@ cdef class tensor: dtype = np.int64 if dtype == float: dtype = np.float64 - # np.bool doesnot have itemsize - if (self.dtype != np.bool and dtype != np.bool) and self.itemsize > dtype.itemsize: + # np.bool_ doesnot have itemsize + if (self.dtype != np.bool_ and dtype != np.bool_) and self.itemsize > dtype.itemsize: raise ValueError("Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.dtype,dtype)) - if dtype == np.bool and self.dtype != np.bool: + if dtype == np.bool_ and self.dtype != np.bool_: raise ValueError("Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'safe'".format(self.dtype,dtype)) str_self = str(self.dtype) str_dtype = str(dtype) @@ -1670,7 +1677,7 @@ cdef class tensor: raise ValueError("Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'same_kind'".format(self.dtype,dtype)) if 'complex' in str_self and ('int' in str_dtype or ('float' in str_dtype)): raise ValueError("Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'same_kind'".format(self.dtype,dtype)) - if self.dtype != np.bool and dtype == np.bool: + if self.dtype != np.bool_ and dtype == np.bool_: raise ValueError("Cannot cast array from dtype({0}) to dtype({1}) according to the rule 'same_kind'".format(self.dtype,dtype)) else: raise ValueError("casting must be one of 'no', 'equiv', 'safe', 'same_kind', or 'unsafe'") @@ -1691,17 +1698,9 @@ cdef class tensor: cdef int64_t * cinds cdef char * cdata cdef int64_t n - self.dt.read_local(&n,&cdata,unpack_sym) - inds = np.empty(n, dtype=np.int64) - vals = np.empty(n, dtype=self.dtype) - - cdef cnp.ndarray buf = np.empty(len(inds), dtype=np.dtype([('a','i8'),('b',self.dtype)],align=ctf.helper._use_align_for_pair(self.dtype))) - d = buf.data - buf.data = cdata - vals[:] = buf['b'][:] - inds[:] = buf['a'][:] - buf.data = d - delete_pairs(self.dt, cdata) + self.dt.read_local(&n,&cinds,&cdata,unpack_sym) + inds = _cast_carray_as_python(n, cinds, np.int64) + vals = _cast_carray_as_python(n, cdata, self.dtype) return inds, vals def dot(self, other, out=None): @@ -1793,16 +1792,9 @@ cdef class tensor: cdef int64_t * cinds cdef char * cdata cdef int64_t n - self.dt.read_local_nnz(&n,&cdata,unpack_sym) - inds = np.empty(n, dtype=np.int64) - vals = np.empty(n, dtype=self.dtype) - cdef cnp.ndarray buf = np.empty(len(inds), dtype=np.dtype([('a','i8'),('b',self.dtype)],align=ctf.helper._use_align_for_pair(self.dtype))) - d = buf.data - buf.data = cdata - vals[:] = buf['b'][:] - inds[:] = buf['a'][:] - buf.data = d - delete_arr(self.dt, cdata) + self.dt.read_local_nnz(&n,&cinds,&cdata,unpack_sym) + inds = _cast_carray_as_python(n, cinds, np.int64) + vals = _cast_carray_as_python(n, cdata, self.dtype) return inds, vals def tot_size(self, unpack=True): @@ -1839,17 +1831,11 @@ cdef class tensor: return cvals = malloc(sz*tB) self.dt.allread(&sz, cvals, unpack) - cdef cnp.ndarray buf = np.empty(sz, dtype=self.dtype) - odata = buf.data - buf.data = cvals + vals = _cast_carray_as_python(sz, cvals, self.dtype) if arr is None: - sbuf = np.asarray(buf) - free(odata) - return buf + return vals else: - arr[:] = buf[:] - free(cvals) - buf.data = odata + arr[:] = vals[:] def read_all_nnz(self, unpack=True): """ @@ -1869,16 +1855,9 @@ cdef class tensor: cdef int64_t * cinds cdef char * cdata cdef int64_t n - cdata = self.dt.read_all_pairs(&n, unpack, True) - inds = np.empty(n, dtype=np.int64) - vals = np.empty(n, dtype=self.dtype) - cdef cnp.ndarray buf = np.empty(len(inds), dtype=np.dtype([('a','i8'),('b',self.dtype)],align=ctf.helper._use_align_for_pair(self.dtype))) - d = buf.data - buf.data = cdata - vals[:] = buf['b'][:] - inds[:] = buf['a'][:] - buf.data = d - delete_arr(self.dt, cdata) + self.dt.read_all_pairs(&n, unpack, &cinds, &cdata, True) + inds = _cast_carray_as_python(n, cinds, np.int64) + vals = _cast_carray_as_python(n, cdata, self.dtype) return inds, vals def __read_all(self, arr): @@ -1893,11 +1872,8 @@ cdef class tensor: sz = self.dt.get_tot_size(False) tB = arr.dtype.itemsize self.dt.get_raw_data(&cvals, &sz) - cdef cnp.ndarray buf = np.empty(sz, dtype=self.dtype) - odata = buf.data - buf.data = cvals - arr[:] = buf[:] - buf.data = odata + vals = _cast_carray_as_python(sz, cvals, self.dtype) + arr[:] = vals[:] def __write_all(self, arr): """ @@ -1911,12 +1887,9 @@ cdef class tensor: sz = self.dt.get_tot_size(False) tB = arr.dtype.itemsize self.dt.get_raw_data(&cvals, &sz) - cdef cnp.ndarray buf = np.empty(sz, dtype=self.dtype) - odata = buf.data - buf.data = cvals + vals = _cast_carray_as_python(sz, cvals, self.dtype) rarr = arr.ravel() - buf[:] = rarr[:] - buf.data = odata + vals[:] = rarr[:] def conj(self): """ @@ -2058,8 +2031,8 @@ cdef class tensor: # cdef cnp.ndarray buf = np.empty(len(iinds), dtype=np.dtype([('a','i8'),('b',self.dtype)],align=False)) cdef char * alpha cdef char * beta - # if type is np.bool, assign the st with 1, since bool does not have itemsize in numpy - if self.dtype == np.bool: + # if type is np.bool_, assign the st with 1, since bool does not have itemsize in numpy + if self.dtype == np.bool_: st = 1 else: st = self.itemsize @@ -2086,6 +2059,7 @@ cdef class tensor: free(alpha) if b is not None: free(beta) + put = write def _get_slice(self, offsets, ends): cdef char * alpha @@ -2257,7 +2231,7 @@ cdef class tensor: else: self.write([],[]) return - if isinstance(value, (np.int, np.float, np.complex, np.number)): + if isinstance(value, (int, float, complex, np.number)): tval = np.asarray([value],dtype=self.dtype)[0] else: tval = ctf.tensor_aux.astensor(value,dtype=self.dtype) @@ -2659,7 +2633,7 @@ cdef class tensor: else: new_shape.append(b.shape[i]) - c = tensor(new_shape, dtype=np.bool, sp=self.sp) + c = tensor(new_shape, dtype=np.bool_, sp=self.sp) # < if op == 0: c.dt.elementwise_smaller(self.dt,b.dt) @@ -2690,3 +2664,4 @@ cdef class tensor: return c +ndarray = tensor diff --git a/src_python/ctf/tensor_aux.pyx b/src_python/ctf/tensor_aux.pyx index 29ad9417..4d3b3be6 100644 --- a/src_python/ctf/tensor_aux.pyx +++ b/src_python/ctf/tensor_aux.pyx @@ -14,6 +14,7 @@ from ctf.chelper cimport * from ctf.tensor cimport tensor, ctensor from ctf.profile import timer from ctf.linalg import svd +from ctf.term cimport itensor cdef extern from "../ctf_ext.h" namespace "CTF_int": cdef void pow_helper[dtype](ctensor * A, ctensor * B, ctensor * C, char * idx_A, char * idx_B, char * idx_C); @@ -328,14 +329,14 @@ def diagonal(init_A, offset=0, axis1=0, axis2=1): if len(dim) == 2: if offset > 0: if dim[0] == dim[1]: - up_left = np.zeros([2], dtype=np.int) + up_left = np.zeros([2], dtype=int) up_left[0] += offset - down_right = np.array([dim[0], dim[1]], dtype=np.int) + down_right = np.array([dim[0], dim[1]], dtype=int) down_right[1] -= offset else: - up_left = np.zeros([2], dtype=np.int) + up_left = np.zeros([2], dtype=int) m = min(dim[0], dim[1]) - down_right = np.array([m, m], dtype=np.int) + down_right = np.array([m, m], dtype=int) up_left[0] += offset down_right[0] += offset if down_right[0] > dim[1]: @@ -344,14 +345,14 @@ def diagonal(init_A, offset=0, axis1=0, axis2=1): return einsum("ii->i",A._get_slice(up_left, down_right)) elif offset <= 0: if dim[0] == dim[1]: - up_left = np.zeros([2], dtype=np.int) + up_left = np.zeros([2], dtype=int) up_left[1] -= offset - down_right = np.array([dim[0], dim[1]], dtype=np.int) + down_right = np.array([dim[0], dim[1]], dtype=int) down_right[0] += offset else: - up_left = np.zeros([2], dtype=np.int) + up_left = np.zeros([2], dtype=int) m = min(dim[0], dim[1]) - down_right = np.array([m, m], dtype=np.int) + down_right = np.array([m, m], dtype=int) up_left[1] -= offset down_right[1] -= offset if down_right[1] > dim[0]: @@ -514,9 +515,6 @@ def take(init_A, indices, axis=None, out=None, mode='raise'): if axis > len(A.shape): raise IndexError("CTF PYTHON ERROR: axis out of bounds") if indices.shape == () or indices.shape== (1,): - total_size = 1 - for i in range(len(A.shape)): - total_size *= A[i] if indices >= A.shape[axis]: raise IndexError("CTF PYTHON ERROR: index out of bounds") ret_shape = list(A.shape) @@ -542,9 +540,6 @@ def take(init_A, indices, axis=None, out=None, mode='raise'): else: if len(indices.shape) > 1: raise ValueError("CTF PYTHON ERROR: current ctf does not support when specify axis and the len(indices.shape) > 1") - total_size = 1 - for i in range(len(A.shape)): - total_size *= A[i] for i in range(len(indices)): if indices[i] >= A.shape[axis]: raise IndexError("index out of bounds") @@ -724,8 +719,8 @@ def dot(tA, tB, out=None): if out is not None: raise ValueError("CTF PYTHON ERROR: CTF currently does not support output parameter.") - if (isinstance(tA, (np.int, np.float, np.complex, np.number)) and - isinstance(tB, (np.int, np.float, np.complex, np.number))): + if (isinstance(tA, (int, float, complex, np.number)) and + isinstance(tB, (int, float, complex, np.number))): return tA * tB A = astensor(tA) @@ -1002,7 +997,7 @@ def any(tensor init_A, axis=None, out=None, keepdims=None): dims_keep = tuple(dims_keep) if out is not None and out.shape != dims_keep: raise ValueError('CTF PYTHON ERROR: output must match when keepdims = True') - B = tensor((1,), dtype=np.bool) + B = tensor((1,), dtype=np.bool_) index_A = _get_num_str(len(A.shape)) if A.get_type() == np.float64: any_helper[double](A.dt, B.dt, index_A.encode(), "".encode()) @@ -1014,21 +1009,21 @@ def any(tensor init_A, axis=None, out=None, keepdims=None): any_helper[int16_t](A.dt, B.dt, index_A.encode(), "".encode()) elif A.get_type() == np.int8: any_helper[int8_t](A.dt, B.dt, index_A.encode(), "".encode()) - elif A.get_type() == np.bool: + elif A.get_type() == np.bool_: any_helper[bool](A.dt, B.dt, index_A.encode(), "".encode()) - if out is not None and out.get_type() != np.bool: + if out is not None and out.get_type() != np.bool_: C = tensor((1,), dtype=out.dtype) B._convert_type(C) vals = C.read([0]) return vals[0] - elif out is not None and keepdims == True and out.get_type() != np.bool: + elif out is not None and keepdims == True and out.get_type() != np.bool_: C = tensor(dims_keep, dtype=out.dtype) B._convert_type(C) return C elif out is None and keepdims == True: ret = reshape(B,dims_keep) return ret - elif out is not None and keepdims == True and out.get_type() == np.bool: + elif out is not None and keepdims == True and out.get_type() == np.bool_: ret = reshape(B,dims_keep) return ret else: @@ -1062,7 +1057,7 @@ def any(tensor init_A, axis=None, out=None, keepdims=None): index_temp = _rev_array(index_A) index_B = index_temp[0:axis] + index_temp[axis+1:len(dim)] index_B = _rev_array(index_B) - B = tensor(dim_ret, dtype=np.bool) + B = tensor(dim_ret, dtype=np.bool_) if A.get_type() == np.float64: any_helper[double](A.dt, B.dt, index_A.encode(), index_B.encode()) elif A.get_type() == np.int64: @@ -1073,7 +1068,7 @@ def any(tensor init_A, axis=None, out=None, keepdims=None): any_helper[int16_t](A.dt, B.dt, index_A.encode(), index_B.encode()) elif A.get_type() == np.int8: any_helper[int8_t](A.dt, B.dt, index_A.encode(), index_B.encode()) - elif A.get_type() == np.bool: + elif A.get_type() == np.bool_: any_helper[bool](A.dt, B.dt, index_A.encode(), index_B.encode()) if out is not None: if out.dtype != B.get_type(): @@ -1115,7 +1110,7 @@ def any(tensor init_A, axis=None, out=None, keepdims=None): for i in range(len(dim_ret)): if dim_ret[i] != out.shape[i]: raise ValueError('CTF PYTHON ERROR: output parameter dimensions mismatch') - B = tensor(dim_ret, dtype=np.bool) + B = tensor(dim_ret, dtype=np.bool_) index_A = _get_num_str(len(dim)) index_temp = _rev_array(index_A) index_B = "" @@ -1133,7 +1128,7 @@ def any(tensor init_A, axis=None, out=None, keepdims=None): any_helper[int16_t](A.dt, B.dt, index_A.encode(), index_B.encode()) elif A.get_type() == np.int8: any_helper[int8_t](A.dt, B.dt, index_A.encode(), index_B.encode()) - elif A.get_type() == np.bool: + elif A.get_type() == np.bool_: any_helper[bool](A.dt, B.dt, index_A.encode(), index_B.encode()) if out is not None: if out.dtype != B.get_type(): @@ -1320,7 +1315,7 @@ def transpose(init_A, axes=None): # length of axes should match with the length of tensor dimension if len(axes) != len(dim): raise ValueError("axes don't match tensor") - axes = np.asarray(axes,dtype=np.int) + axes = np.asarray(axes,dtype=int) for i in range(A.ndim): if axes[i] < 0: axes[i] = A.ndim+axes[i] @@ -1396,7 +1391,7 @@ def ones(shape, dtype = None, order='F'): string_index += 1 if dtype == np.float64 or dtype == np.complex128 or dtype == np.complex64 or dtype == np.float32: ret.i(string) << 1.0 - elif dtype == np.bool or dtype == np.int64 or dtype == np.int32 or dtype == np.int16 or dtype == np.int8: + elif dtype == np.bool_ or dtype == np.int64 or dtype == np.int32 or dtype == np.int16 or dtype == np.int8: ret.i(string) << 1 else: raise ValueError('CTF PYTHON ERROR: bad dtype') @@ -1460,7 +1455,7 @@ def eye(n, m=None, k=0, dtype=np.float64, sp=False): A = tensor([l, l], dtype=dtype, sp=sp) if dtype == np.float64 or dtype == np.complex128 or dtype == np.complex64 or dtype == np.float32: A.i("ii") << 1.0 - elif dtype == np.bool or dtype == np.int64 or dtype == np.int32 or dtype == np.int16 or dtype == np.int8: + elif dtype == np.bool_ or dtype == np.int64 or dtype == np.int32 or dtype == np.int16 or dtype == np.int8: A.i("ii") << 1 else: raise ValueError('CTF PYTHON ERROR: bad dtype') @@ -1943,7 +1938,7 @@ def arange(start, stop, step=1, dtype=None): vec_arange[int16_t]((t.dt), start, stop, step) elif dtype == np.int8: vec_arange[int8_t]((t.dt), start, stop, step) - elif dtype == np.bool: + elif dtype == np.bool_: vec_arange[bool]((t.dt), start, stop, step) else: raise ValueError('CTF PYTHON ERROR: unsupported starting value type for numpy arange') @@ -2479,7 +2474,9 @@ def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe operand = new_operands[0].i(inds[0]) for i in range(1,numop): operand = operand * new_operands[i].i(inds[i]) - out_scale*output.i(out_inds) << operand + itsr = output.i(out_inds) + itsr.scale(out_scale) + itsr << operand if out is None: if len(out_inds) == 0: output = output.item() @@ -2734,7 +2731,7 @@ def all(inA, axis=None, out=None, keepdims = False): else: if isinstance(inA, np.ndarray): return np.all(inA,axis,out,keepdims) - if isinstance(inA, np.bool): + if isinstance(inA, np.bool_): return inA else: raise ValueError('CTF PYTHON ERROR: ctf.all called on invalid operand') @@ -2752,5 +2749,3 @@ def _comp_all(tensor A, axis=None, out=None, keepdims=None): if axis is None: x = A._bool_sum() return x == A.tot_size() - - diff --git a/src_python/ctf/term.pyx b/src_python/ctf/term.pyx index 6c46ca51..60f243ac 100644 --- a/src_python/ctf/term.pyx +++ b/src_python/ctf/term.pyx @@ -49,7 +49,7 @@ cdef class term: return self.dtype def scale(self, scl): - if isinstance(scl, (np.int, np.float, np.double, np.number)): + if isinstance(scl, (int, float, np.double, np.number)): tm_old = self.tm self.tm = (deref(self.tm) * scl).clone() if tm_old != self.tm: @@ -254,9 +254,9 @@ cdef class itensor(term): tensor_svd_cmplx(self.tsr.dt, self.string.encode(), VT_string.encode(), U_string.encode(), rank, threshold, use_svd_rand, num_iter, oversamp, ctsrs) else: raise ValueError('CTF PYTHON ERROR: SVD must be called on real or complex single/double precision tensor') - cdef cnp.ndarray lens_U = cnp.ndarray(ctsrs[2].order,dtype=np.int) - cdef cnp.ndarray lens_S = cnp.ndarray(ctsrs[1].order,dtype=np.int) - cdef cnp.ndarray lens_VT = cnp.ndarray(ctsrs[0].order,dtype=np.int) + cdef cnp.ndarray lens_U = cnp.ndarray(ctsrs[2].order,dtype=int) + cdef cnp.ndarray lens_S = cnp.ndarray(ctsrs[1].order,dtype=int) + cdef cnp.ndarray lens_VT = cnp.ndarray(ctsrs[0].order,dtype=int) for i in range(ctsrs[0].order): lens_VT[i] = ctsrs[0].lens[i] if _ord_comp(self.tsr.order, 'F'): diff --git a/test/python/test_base.py b/test/python/test_base.py index d114c885..db11cdca 100644 --- a/test/python/test_base.py +++ b/test/python/test_base.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import unittest -import numpy +import numpy as np import ctf import os import sys @@ -12,20 +12,20 @@ def test_eye(self): a0 = ctf.identity(4) a1 = ctf.eye(4) self.assertTrue(ctf.all(a0==a1)) - a1 = ctf.eye(4, dtype=numpy.complex128) - self.assertTrue(a1.dtype == numpy.complex128) + a1 = ctf.eye(4, dtype=np.complex128) + self.assertTrue(a1.dtype == np.complex128) def test_zeros(self): a1 = ctf.zeros((2,3,4)) - a1 = ctf.zeros((2,3,4), dtype=numpy.complex128) + a1 = ctf.zeros((2,3,4), dtype=np.complex128) a1 = ctf.zeros_like(a1) - self.assertTrue(a1.dtype == numpy.complex128) + self.assertTrue(a1.dtype == np.complex128) def test_empty(self): a1 = ctf.empty((2,3,4)) - a1 = ctf.empty((2,3,4), dtype=numpy.complex128) + a1 = ctf.empty((2,3,4), dtype=np.complex128) a1 = ctf.empty_like(a1) - self.assertTrue(a1.dtype == numpy.complex128) + self.assertTrue(a1.dtype == np.complex128) def test_copy(self): a1 = ctf.zeros((2,3,4)) @@ -33,12 +33,12 @@ def test_copy(self): a1 = a1.copy() def test_sum(self): - a0 = numpy.arange(4.) + a0 = np.arange(4.) a1 = ctf.from_nparray(a0) self.assertAlmostEqual(ctf.sum(a1), a1.sum(), 9) def test_sum_axis(self): - a0 = numpy.ones((2,3,4)) + a0 = np.ones((2,3,4)) a1 = ctf.from_nparray(a0) self.assertEqual(a1.sum(axis=0).shape, (3,4)) self.assertEqual(a1.sum(axis=1).shape, (2,4)) @@ -56,20 +56,20 @@ def test_astensor(self): a0 = ctf.astensor((1,2,3)) a0 = ctf.astensor([1,2.,3]) a0 = ctf.astensor([(1,2), (3,4)]) - a0 = ctf.astensor(numpy.arange(3)) - a0 = ctf.astensor([numpy.array((1,2)), numpy.array((3,4))+1j]) + a0 = ctf.astensor(np.arange(3)) + a0 = ctf.astensor([np.array((1,2)), np.array((3,4))+1j]) a1 = ctf.astensor(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) - a0 = numpy.array(a1) #FIXME: this breaks __len__ + a0 = np.array(a1) #FIXME: this breaks __len__ a0 = ctf.astensor([1,2.,3], dtype='D') - self.assertTrue(a0.dtype == numpy.complex128) + self.assertTrue(a0.dtype == np.complex128) with self.assertRaises(TypeError): ctf.astensor([1j,2j], dtype='d') - a0 = numpy.arange(4.).reshape(2,2) + a0 = np.arange(4.).reshape(2,2) a1 = ctf.to_nparray(ctf.from_nparray(a0)) self.assertTrue(ctf.all(a0==a1)) try: @@ -78,26 +78,26 @@ def test_astensor(self): except AttributeError: pass - a0 = ctf.from_nparray(numpy.arange(3)) + a0 = ctf.from_nparray(np.arange(3)) a1 = ctf.from_nparray(a0) a1[:] = 0 self.assertTrue(ctf.all(a0==0)) self.assertTrue(ctf.all(a1==0)) - a0 = numpy.arange(6).reshape(2,3) + a0 = np.arange(6).reshape(2,3) a1 = ctf.array(a0) self.assertTrue(ctf.all(a1==a0)) a1 = ctf.array(a0, copy=False) self.assertTrue(ctf.all(a1==0)) def test_transpose_astensor(self): - a0 = numpy.arange(6).reshape(2,3) + a0 = np.arange(6).reshape(2,3) a1 = ctf.astensor(a0.T) #self.assertTrue(ctf.all(a1==a0)) a1 = ctf.astensor(a1.T()) self.assertTrue(ctf.all(a1==a0)) - a0 = numpy.arange(120).reshape(2,3,4,5) + a0 = np.arange(120).reshape(2,3,4,5) a1 = a0.transpose(0,1,3,2) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) a1 = a0.transpose(0,2,1,3) @@ -107,7 +107,7 @@ def test_transpose_astensor(self): a1 = a0.transpose(2,1,0,3) self.assertTrue(ctf.all(ctf.astensor(a1)==a1)) - a0 = numpy.arange(120).reshape(2,3,4,5) + a0 = np.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(a1.transpose(0,1,3,2)==a0.transpose(0,1,3,2))) self.assertTrue(ctf.all(a1.transpose(0,2,1,3)==a0.transpose(0,2,1,3))) @@ -116,25 +116,25 @@ def test_transpose_astensor(self): def test_astype(self): a0 = ctf.zeros((2,3)) - self.assertTrue(a0.astype(numpy.complex128).dtype == numpy.complex128) - self.assertTrue(a0.astype('D').dtype == numpy.complex128) - self.assertTrue(a0.real().dtype == numpy.double) - self.assertTrue(a0.imag().dtype == numpy.double) + self.assertTrue(a0.astype(np.complex128).dtype == np.complex128) + self.assertTrue(a0.astype('D').dtype == np.complex128) + self.assertTrue(a0.real().dtype == np.double) + self.assertTrue(a0.imag().dtype == np.double) self.assertTrue(a0.real().shape == (2,3)) self.assertTrue(a0.imag().shape == (2,3)) - self.assertTrue(a0.conj().dtype == numpy.double) + self.assertTrue(a0.conj().dtype == np.double) a0 = ctf.zeros((2,3), dtype='D') - self.assertTrue(a0.astype(numpy.double).dtype == numpy.double) - self.assertTrue(a0.astype('d').dtype == numpy.double) - self.assertTrue(a0.real().dtype == numpy.double) - self.assertTrue(a0.imag().dtype == numpy.double) + self.assertTrue(a0.astype(np.double).dtype == np.double) + self.assertTrue(a0.astype('d').dtype == np.double) + self.assertTrue(a0.real().dtype == np.double) + self.assertTrue(a0.imag().dtype == np.double) self.assertTrue(a0.real().shape == (2,3)) self.assertTrue(a0.imag().shape == (2,3)) - self.assertTrue(a0.conj().dtype == numpy.complex128) + self.assertTrue(a0.conj().dtype == np.complex128) def test_ravel(self): - a0 = numpy.arange(120).reshape(2,3,4,5) + a0 = np.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(a1.ravel()==a0.ravel())) #self.assertTrue(ctf.all(a1.transpose(0,1,3,2).ravel()==a0.transpose(0,1,3,2).ravel())) @@ -148,7 +148,7 @@ def test_reshape(self): self.assertTrue(ctf.all(a.reshape((10,100)) == a.to_nparray().reshape((10,100)))) base_shapes = [(2,3,4,5),(3,10,4),(1,3,10,4),(2,3,4,1,5)] for shape in base_shapes: - a0 = numpy.arange(120).reshape(shape) + a0 = np.arange(120).reshape(shape) a1 = ctf.astensor(a0) self.assertTrue(ctf.all(ctf.reshape(a1,(2,3,4,5)) ==a0.reshape(2,3,4,5))) self.assertTrue(ctf.all(ctf.reshape(a1,(6,20)) ==a0.reshape(6,20))) @@ -214,7 +214,7 @@ def test_sp_reshape(self): #self.assertTrue(ctf.all(a1==0)) def test_transpose_reshape(self): - a0 = numpy.arange(120).reshape(2,3,4,5) + a0 = np.arange(120).reshape(2,3,4,5) a1 = ctf.astensor(a0) a0 = a0.transpose(3,0,2,1) a1 = a1.transpose(3,0,2,1) @@ -276,7 +276,7 @@ def test_attributes(self): self.assertTrue(a0.shape == (2,3,4,5)) self.assertTrue(a0.T().shape == (5,4,3,2)) self.assertTrue(a0.size == 120) - self.assertTrue(a0.dtype == numpy.double) + self.assertTrue(a0.dtype == np.double) self.assertTrue(a0.real().shape == (2,3,4,5)) self.assertTrue(a0.imag().shape == (2,3,4,5)) self.assertTrue(a0.ndim == 4) @@ -294,10 +294,10 @@ def test_attributes(self): #self.assertTrue(ctf.all(a0 == 1+1j)) def test_diagonal(self): - a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) + a0 = ctf.astensor(np.arange(9).reshape(3,3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.astensor([0,4,8]))) - self.assertTrue(ctf.all(a1 == ctf.diagonal(numpy.arange(9).reshape(3,3)))) + self.assertTrue(ctf.all(a1 == ctf.diagonal(np.arange(9).reshape(3,3)))) try: a1.diagonal() except ValueError: # a1 needs to be 2d array @@ -305,157 +305,157 @@ def test_diagonal(self): # support dimensions > 2d or not? def test_diag(self): - a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) + a0 = ctf.astensor(np.arange(9).reshape(3,3)) a1 = a0.diagonal() self.assertTrue(ctf.all(a1 == ctf.diag(a0))) - self.assertTrue(ctf.all(ctf.diag(a1) == numpy.diag(numpy.arange(9).reshape(3,3).diagonal()))) + self.assertTrue(ctf.all(ctf.diag(a1) == np.diag(np.arange(9).reshape(3,3).diagonal()))) def test_trace(self): - a0 = ctf.astensor(numpy.arange(9).reshape(3,3)) + a0 = ctf.astensor(np.arange(9).reshape(3,3)) a1 = a0.trace() self.assertEqual(a1, 12) - self.assertEqual(ctf.trace(numpy.arange(9).reshape(3,3)), 12) + self.assertEqual(ctf.trace(np.arange(9).reshape(3,3)), 12) def test_take(self): - a0 = numpy.arange(24.).reshape(4,3,2) + a0 = np.arange(24.).reshape(4,3,2) a1 = ctf.astensor(a0) - self.assertEqual(ctf.take(a0, numpy.array([0,3]), axis=0).shape, (2,3,2)) + self.assertEqual(ctf.take(a0, np.array([0,3]), axis=0).shape, (2,3,2)) self.assertEqual(ctf.take(a1, [2], axis=1).shape, (4,1,2)) self.assertEqual(a1.take([0], axis=-1).shape, (4,3,1)) def test_vstack(self): - a1 = ctf.astensor(numpy.ones(4)) - a2 = ctf.astensor(numpy.ones(4)) + a1 = ctf.astensor(np.ones(4)) + a2 = ctf.astensor(np.ones(4)) self.assertTrue(ctf.vstack((a1, a2)).shape == (2,4)) - a1 = ctf.astensor(numpy.ones((2,4))) - a2 = ctf.astensor(numpy.ones((3,4))) + a1 = ctf.astensor(np.ones((2,4))) + a2 = ctf.astensor(np.ones((3,4))) self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4)) - a1 = ctf.astensor(numpy.ones((2,4))) - a2 = ctf.astensor(numpy.ones((3,4))+0j) + a1 = ctf.astensor(np.ones((2,4))) + a2 = ctf.astensor(np.ones((3,4))+0j) self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4)) - self.assertTrue(ctf.vstack((a1, a2)).dtype == numpy.complex128) + self.assertTrue(ctf.vstack((a1, a2)).dtype == np.complex128) - a1 = ctf.astensor(numpy.ones((4,1))) + a1 = ctf.astensor(np.ones((4,1))) self.assertTrue(ctf.vstack((a1, 1.5)).shape == (5,1)) - a1 = ctf.astensor(numpy.ones((2,4,2))) - a2 = ctf.astensor(numpy.ones((3,4,2))) + a1 = ctf.astensor(np.ones((2,4,2))) + a2 = ctf.astensor(np.ones((3,4,2))) self.assertTrue(ctf.vstack((a1, a2)).shape == (5,4,2)) def test_hstack(self): - a1 = ctf.astensor(numpy.ones(4)) - a2 = ctf.astensor(numpy.ones(5)) + a1 = ctf.astensor(np.ones(4)) + a2 = ctf.astensor(np.ones(5)) self.assertTrue(ctf.hstack((a1, a2)).shape == (9,)) - a1 = ctf.astensor(numpy.ones((2,4))) - a2 = ctf.astensor(numpy.ones((2,5))) + a1 = ctf.astensor(np.ones((2,4))) + a2 = ctf.astensor(np.ones((2,5))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) - a1 = ctf.astensor(numpy.ones((2,4))) - a2 = ctf.astensor(numpy.ones((2,5))+0j) + a1 = ctf.astensor(np.ones((2,4))) + a2 = ctf.astensor(np.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) - self.assertTrue(ctf.hstack((a1, a2)).dtype == numpy.complex128) + self.assertTrue(ctf.hstack((a1, a2)).dtype == np.complex128) - a1 = numpy.ones((2,4)) - a2 = ctf.astensor(numpy.ones((2,5))+0j) + a1 = np.ones((2,4)) + a2 = ctf.astensor(np.ones((2,5))+0j) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9)) - na2 = numpy.ones((2,5))+0j - self.assertTrue(ctf.all(ctf.hstack((a1, a2)) == numpy.hstack((a1,na2)))) + na2 = np.ones((2,5))+0j + self.assertTrue(ctf.all(ctf.hstack((a1, a2)) == np.hstack((a1,na2)))) - a1 = ctf.astensor(numpy.ones(4)) + a1 = ctf.astensor(np.ones(4)) self.assertTrue(ctf.hstack((a1, 1.5)).shape == (5,)) - a1 = ctf.astensor(numpy.ones((2,4,2))) - a2 = ctf.astensor(numpy.ones((2,5,2))) + a1 = ctf.astensor(np.ones((2,4,2))) + a2 = ctf.astensor(np.ones((2,5,2))) self.assertTrue(ctf.hstack((a1, a2)).shape == (2,9,2)) def test_int_conv(self): a = ctf.ones(2, dtype=float) - b = numpy.ones(2, dtype=float) - self.assertTrue(numpy.allclose(a.to_nparray(),b)) + b = np.ones(2, dtype=float) + self.assertTrue(np.allclose(a.to_nparray(),b)) a = ctf.zeros(2, dtype=complex) a[0] = 1 - self.assertTrue(numpy.allclose([a.norm2()],[1.])) + self.assertTrue(np.allclose([a.norm2()],[1.])) a *= 2 - self.assertTrue(numpy.allclose([a.norm2()],[2.])) + self.assertTrue(np.allclose([a.norm2()],[2.])) def test_rounding(self): - for dt in [numpy.float32, numpy.float64]: - a = numpy.arange(-5.,5.,.2,dtype=dt) - fa = numpy.floor(a) - ra = numpy.rint(a) - ca = numpy.ceil(a) + for dt in [np.float32, np.float64]: + a = np.arange(-5.,5.,.2,dtype=dt) + fa = np.floor(a) + ra = np.rint(a) + ca = np.ceil(a) cfa = ctf.floor(a) cra = ctf.rint(a) cca = ctf.ceil(a) - self.assertTrue(numpy.allclose(cfa.to_nparray(),fa)) + self.assertTrue(np.allclose(cfa.to_nparray(),fa)) #Note this won't generallybe true due to different rounding rules e.g. for -3.5 - self.assertTrue(numpy.allclose(cra.to_nparray(),ra)) - self.assertTrue(numpy.allclose(cca.to_nparray(),ca)) + self.assertTrue(np.allclose(cra.to_nparray(),ra)) + self.assertTrue(np.allclose(cca.to_nparray(),ca)) def test_clip(self): - a = numpy.random.rand(10,5) + a = np.random.rand(10,5) b = ctf.astensor(a) - a_clipped = numpy.clip(a,0.3,0.7) + a_clipped = np.clip(a,0.3,0.7) b_clipped = ctf.clip(b,0.3,0.7) - self.assertTrue(numpy.allclose(b_clipped.to_nparray(),a_clipped)) + self.assertTrue(np.allclose(b_clipped.to_nparray(),a_clipped)) def test_complex(self): - for dt in [numpy.complex64, numpy.complex128, complex]: - a = abs(numpy.ones(2, dtype=dt)) + for dt in [np.complex64, np.complex128, complex]: + a = abs(np.ones(2, dtype=dt)) b = abs(ctf.ones(2, dtype=dt)) self.assertTrue(a.dtype==b.dtype) - self.assertTrue(numpy.allclose(a,b.to_nparray())) + self.assertTrue(np.allclose(a,b.to_nparray())) def test_mixtype_comp(self): - a = numpy.ones(2, dtype=int) < 1.0 + a = np.ones(2, dtype=int) < 1.0 b = ctf.ones(2, dtype=int) < 1.0 - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.zeros(2, dtype=numpy.float32) < numpy.ones(2, dtype=numpy.float64) - b = ctf.zeros(2, dtype=numpy.float32) < ctf.ones(2, dtype=numpy.float64) - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.zeros(2, dtype=np.float32) < np.ones(2, dtype=np.float64) + b = ctf.zeros(2, dtype=np.float32) < ctf.ones(2, dtype=np.float64) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.ones(2, dtype=int) == np.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) - self.assertTrue(numpy.all(a==b.to_nparray())) + self.assertTrue(np.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) <= 1.0 + a = np.ones(2, dtype=int) <= 1.0 b = ctf.ones(2, dtype=int) <= 1.0 - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.zeros(2, dtype=numpy.float32) <= numpy.ones(2, dtype=numpy.float64) - b = ctf.zeros(2, dtype=numpy.float32) <= ctf.ones(2, dtype=numpy.float64) - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.zeros(2, dtype=np.float32) <= np.ones(2, dtype=np.float64) + b = ctf.zeros(2, dtype=np.float32) <= ctf.ones(2, dtype=np.float64) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.ones(2, dtype=int) == np.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) - self.assertTrue(numpy.all(a==b.to_nparray())) + self.assertTrue(np.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) > 1.0 + a = np.ones(2, dtype=int) > 1.0 b = ctf.ones(2, dtype=int) > 1.0 - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.zeros(2, dtype=numpy.float32) > numpy.ones(2, dtype=numpy.float64) - b = ctf.zeros(2, dtype=numpy.float32) > ctf.ones(2, dtype=numpy.float64) - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.zeros(2, dtype=np.float32) > np.ones(2, dtype=np.float64) + b = ctf.zeros(2, dtype=np.float32) > ctf.ones(2, dtype=np.float64) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.ones(2, dtype=int) == np.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) - self.assertTrue(numpy.all(a==b.to_nparray())) + self.assertTrue(np.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) >= 1.0 + a = np.ones(2, dtype=int) >= 1.0 b = ctf.ones(2, dtype=int) >= 1.0 - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.zeros(2, dtype=numpy.float32) >= numpy.ones(2, dtype=numpy.float64) - b = ctf.zeros(2, dtype=numpy.float32) >= ctf.ones(2, dtype=numpy.float64) - self.assertTrue(numpy.all(a==b.to_nparray())) - a = numpy.ones(2, dtype=int) == numpy.ones(2, dtype=float) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.zeros(2, dtype=np.float32) >= np.ones(2, dtype=np.float64) + b = ctf.zeros(2, dtype=np.float32) >= ctf.ones(2, dtype=np.float64) + self.assertTrue(np.all(a==b.to_nparray())) + a = np.ones(2, dtype=int) == np.ones(2, dtype=float) b = ctf.ones(2, dtype=int) == ctf.ones(2, dtype=float) - self.assertTrue(numpy.all(a==b.to_nparray())) + self.assertTrue(np.all(a==b.to_nparray())) def run_tests(): - numpy.random.seed(5330); + np.random.seed(5330); wrld = ctf.comm() if ctf.comm().rank() != 0: result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(KnowValues))) diff --git a/test/python/test_einsum.py b/test/python/test_einsum.py index 8a112579..c8a907b4 100644 --- a/test/python/test_einsum.py +++ b/test/python/test_einsum.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import unittest -import numpy +import numpy as np import ctf import os import sys @@ -13,127 +13,127 @@ def allclose(a, b): class KnowValues(unittest.TestCase): def test_einsum_views(self): - a0 = numpy.arange(27.).reshape(3,3,3) + a0 = np.arange(27.).reshape(3,3,3) a1 = ctf.astensor(a0) - self.assertTrue(allclose(ctf.einsum("jii->ij", a1), numpy.einsum("jii->ij", a0))) - self.assertTrue(allclose(ctf.einsum("iii->i", a1), numpy.einsum("iii->i", a0))) - self.assertTrue(allclose(ctf.einsum("iii", a1), numpy.einsum("iii", a0))) + self.assertTrue(allclose(ctf.einsum("jii->ij", a1), np.einsum("jii->ij", a0))) + self.assertTrue(allclose(ctf.einsum("iii->i", a1), np.einsum("iii->i", a0))) + self.assertTrue(allclose(ctf.einsum("iii", a1), np.einsum("iii", a0))) - a0 = numpy.arange(6.) + a0 = np.arange(6.) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum("i,i,i->i", a1, a1, a1), - numpy.einsum("i,i,i->i", a0, a0, a0))) + np.einsum("i,i,i->i", a0, a0, a0))) # swap axes - a0 = numpy.arange(24.).reshape(4,3,2) + a0 = np.arange(24.).reshape(4,3,2) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum("ijk->kji", a1), - numpy.einsum("ijk->kji", a0))) + np.einsum("ijk->kji", a0))) def test_einsum_sums(self): # outer(a,b) for n in range(1, 17): - a0 = numpy.arange(3, dtype=numpy.double)+1 - b0 = numpy.arange(n, dtype=numpy.double)+1 + a0 = np.arange(3, dtype=np.double)+1 + b0 = np.arange(n, dtype=np.double)+1 a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) - self.assertTrue(allclose(ctf.einsum("i,j", a1, b1), numpy.outer(a0, b0))) + self.assertTrue(allclose(ctf.einsum("i,j", a1, b1), np.outer(a0, b0))) # matvec(a,b) / a.dot(b) where a is matrix, b is vector for n in range(1, 17): - a0 = numpy.arange(4*n, dtype=numpy.double).reshape(n, 4) - b0 = numpy.arange(n, dtype=numpy.double) + a0 = np.arange(4*n, dtype=np.double).reshape(n, 4) + b0 = np.arange(n, dtype=np.double) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) - self.assertTrue(allclose(ctf.einsum("ji,j", a1, b1), numpy.dot(b0.T, a0))) - self.assertTrue(allclose(ctf.einsum("ji,j->", a1, b1), numpy.dot(b0.T, a0).sum())) + self.assertTrue(allclose(ctf.einsum("ji,j", a1, b1), np.dot(b0.T, a0))) + self.assertTrue(allclose(ctf.einsum("ji,j->", a1, b1), np.dot(b0.T, a0).sum())) # matmat(a,b) / a.dot(b) where a is matrix, b is matrix for n in range(1, 17): - a0 = numpy.arange(4*n, dtype=numpy.double).reshape(n, 4) - b0 = numpy.arange(6*n, dtype=numpy.double).reshape(n, 6) + a0 = np.arange(4*n, dtype=np.double).reshape(n, 4) + b0 = np.arange(6*n, dtype=np.double).reshape(n, 6) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) - self.assertTrue(allclose(ctf.einsum("ji,jk", a1, b1), numpy.dot(a0.T, b0))) - self.assertTrue(allclose(ctf.einsum("ji,jk->", a1, b1), numpy.dot(a0.T, b0).sum())) + self.assertTrue(allclose(ctf.einsum("ji,jk", a1, b1), np.dot(a0.T, b0))) + self.assertTrue(allclose(ctf.einsum("ji,jk->", a1, b1), np.dot(a0.T, b0).sum())) # matrix triple product (note this is not currently an efficient # way to multiply 3 matrices) - a0 = numpy.arange(12.).reshape(3, 4) - b0 = numpy.arange(20.).reshape(4, 5) - c0 = numpy.arange(30.).reshape(5, 6) + a0 = np.arange(12.).reshape(3, 4) + b0 = np.arange(20.).reshape(4, 5) + c0 = np.arange(30.).reshape(5, 6) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) c1 = ctf.astensor(c0) self.assertTrue(allclose(ctf.einsum("ij,jk,kl", a1, b1, c1), - numpy.einsum("ij,jk,kl", a0, b0, c0))) + np.einsum("ij,jk,kl", a0, b0, c0))) # tensordot(a, b) - a0 = numpy.arange(27.).reshape(3, 3, 3) - b0 = numpy.arange(27.).reshape(3, 3, 3) + a0 = np.arange(27.).reshape(3, 3, 3) + b0 = np.arange(27.).reshape(3, 3, 3) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum("ijk, jli -> kl", a1, b1), - numpy.einsum("ijk, jli -> kl", a0, b0))) + np.einsum("ijk, jli -> kl", a0, b0))) self.assertTrue(allclose(ctf.einsum("ijk, jli -> lk", a1, b1), - numpy.einsum("ijk, jli -> lk", a0, b0))) + np.einsum("ijk, jli -> lk", a0, b0))) self.assertTrue(allclose(ctf.einsum("ikj, jli -> kl", a1, b1), - numpy.einsum("ikj, jli -> kl", a0, b0))) + np.einsum("ikj, jli -> kl", a0, b0))) self.assertTrue(allclose(ctf.einsum("kij, lij -> lk", a1, b1), - numpy.einsum("kij, lij -> lk", a0, b0))) + np.einsum("kij, lij -> lk", a0, b0))) def test_einsum_misc(self): # The iterator had an issue with buffering this reduction - a0 = numpy.ones((5, 12, 4, 2, 3)) - b0 = numpy.ones((5, 12, 11)) + a0 = np.ones((5, 12, 4, 2, 3)) + b0 = np.ones((5, 12, 11)) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) self.assertTrue(allclose(ctf.einsum('ijklm,ijn->', a1, b1), - numpy.einsum('ijklm,ijn->', a0, b0))) + np.einsum('ijklm,ijn->', a0, b0))) self.assertTrue(allclose(ctf.einsum('ijklm,ijn,ijn->', a1, b1, b1), - #numpy.einsum('ijklm,ijn,ijn->', a0, b0, b0))) - numpy.einsum('ijklm,ijn->', a0, b0))) + #np.einsum('ijklm,ijn,ijn->', a0, b0, b0))) + np.einsum('ijklm,ijn->', a0, b0))) # inner loop implementation - a0 = numpy.arange(1., 3.) - b0 = numpy.arange(1., 5.).reshape(2, 2) - c0 = numpy.arange(1., 9.).reshape(4, 2) + a0 = np.arange(1., 3.) + b0 = np.arange(1., 5.).reshape(2, 2) + c0 = np.arange(1., 9.).reshape(4, 2) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) c1 = ctf.astensor(c0) self.assertTrue(allclose(ctf.einsum('x,yx,zx->xzy', a1, b1, c1), - numpy.einsum('x,yx,zx->xzy', a0, b0, c0))) + np.einsum('x,yx,zx->xzy', a0, b0, c0))) - a0 = numpy.random.normal(0, 1, (5, 5, 5, 5)) + a0 = np.random.normal(0, 1, (5, 5, 5, 5)) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum('aabb->ab', a1), - numpy.einsum('aabb->ab', a0))) + np.einsum('aabb->ab', a0))) - a0 = numpy.arange(25.).reshape(5, 5) + a0 = np.arange(25.).reshape(5, 5) a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.einsum('mi,mi,mi->m', a1, a1, a1), - numpy.einsum('mi,mi,mi->m', a0, a0, a0))) + np.einsum('mi,mi,mi->m', a0, a0, a0))) def test_einsum_mix_types(self): - a0 = numpy.random.random((5, 1, 4, 2, 3)).astype(numpy.complex)+1j - b0 = numpy.random.random((5, 1, 11)).astype(numpy.float32) + a0 = np.random.random((5, 1, 4, 2, 3)).astype(complex)+1j + b0 = np.random.random((5, 1, 11)).astype(np.float32) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) - c0 = numpy.einsum('ijklm,ijn->', a0, b0) + c0 = np.einsum('ijklm,ijn->', a0, b0) c1 = ctf.einsum('ijklm,ijn->', a1, b1) self.assertTrue(allclose(c0, c1)) c1 = ctf.einsum('ijklm,ijn->', a1, b0) self.assertTrue(allclose(c0, c1)) c1 = ctf.einsum('ijklm,ijn->', a0, b1) self.assertTrue(allclose(c0, c1)) - c0 = numpy.einsum('ijklm,->ij', a0, 3.) + c0 = np.einsum('ijklm,->ij', a0, 3.) c1 = ctf.einsum('ijklm,->ij', a1, 3.) self.assertTrue(allclose(c0, c1)) def test_MTTKRP_vec(self): for N in range(2,5): - lens = numpy.random.randint(3, 4, N) + lens = np.random.randint(3, 4, N) A = ctf.tensor(lens) A.fill_sp_random(-1.,1.,.5) mats = [] @@ -153,7 +153,7 @@ def test_MTTKRP_vec(self): def test_MTTKRP_mat(self): k = 9 for N in range(2,5): - lens = numpy.random.randint(3, 4, N) + lens = np.random.randint(3, 4, N) A = ctf.tensor(lens) A.fill_sp_random(-1.,1.,.5) mats = [] @@ -173,8 +173,8 @@ def test_Solve_Factor_mat(self): R = 10 for N in range(3,6): mats = [] - num = numpy.random.randint(N) - lens = numpy.random.randint(10,20,N) + num = np.random.randint(N) + lens = np.random.randint(10,20,N) for i in range(N): if i !=num: mats.append(ctf.random.random([lens[i],R])) @@ -194,19 +194,19 @@ def test_Solve_Factor_mat(self): lst_mat.append(mats[i].to_nparray()) einstr+= T_inds + "->"+chr(ord('a')+num)+'rz' lst_mat.append(A.to_nparray()) - lhs_np =numpy.einsum(einstr,*lst_mat,optimize=True) + lhs_np =np.einsum(einstr,*lst_mat,optimize=True) rhs_np = RHS.to_nparray() - ans = numpy.zeros_like(rhs_np) + ans = np.zeros_like(rhs_np) for i in range(mats[num].shape[0]): ans[i,:] = la.solve(lhs_np[i],rhs_np[i,:]) ctf.Solve_Factor(A,mats,RHS,num) - self.assertTrue(numpy.allclose(ans, mats[num].to_nparray())) + self.assertTrue(np.allclose(ans, mats[num].to_nparray())) def test_TTTP_vec(self): - A = numpy.random.random((4, 3, 5)) - u = numpy.random.random((4,)) - v = numpy.random.random((5,)) - ans = numpy.einsum("ijk,i,k->ijk",A,u,v) + A = np.random.random((4, 3, 5)) + u = np.random.random((4,)) + v = np.random.random((5,)) + ans = np.einsum("ijk,i,k->ijk",A,u,v) cA = ctf.astensor(A) cu = ctf.astensor(u) cv = ctf.astensor(v) @@ -214,13 +214,13 @@ def test_TTTP_vec(self): self.assertTrue(allclose(ans, cans)) def test_TTTP_mat(self): - A = numpy.random.random((5, 1, 4, 2, 3)) - u = numpy.random.random((5, 3)) - v = numpy.random.random((1, 3)) - w = numpy.random.random((4, 3)) - x = numpy.random.random((2, 3)) - y = numpy.random.random((3, 3)) - ans = numpy.einsum("ijklm,ia,ja,ka,la,ma->ijklm",A,u,v,w,x,y) + A = np.random.random((5, 1, 4, 2, 3)) + u = np.random.random((5, 3)) + v = np.random.random((1, 3)) + w = np.random.random((4, 3)) + x = np.random.random((2, 3)) + y = np.random.random((3, 3)) + ans = np.einsum("ijklm,ia,ja,ka,la,ma->ijklm",A,u,v,w,x,y) cA = ctf.astensor(A) cu = ctf.astensor(u) cv = ctf.astensor(v) @@ -251,11 +251,11 @@ def test_tree_ctr(self): for i in range(1,5): C = C * ctf.dot(X[i],X[5+i]) scl2 = ctf.vecnorm(C,1) - self.assertTrue(numpy.abs(scl-scl2)<1.e-4) + self.assertTrue(np.abs(scl-scl2)<1.e-4) def run_tests(): - numpy.random.seed(5330); + np.random.seed(5330); wrld = ctf.comm() if wrld.rank() != 0: result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(KnowValues))) @@ -268,4 +268,4 @@ def run_tests(): result = run_tests() ctf.MPI_Stop() sys.exit(not result) - \ No newline at end of file + diff --git a/test/python/test_fancyindex.py b/test/python/test_fancyindex.py index e377d661..5b864f24 100644 --- a/test/python/test_fancyindex.py +++ b/test/python/test_fancyindex.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import unittest -import numpy +import numpy as np import ctf import ctf.random import os @@ -11,13 +11,13 @@ def allclose(a, b): return abs(ctf.to_nparray(a) - ctf.to_nparray(b)).sum() < 1e-10 def a0_and_a1(): - a0 = numpy.arange(60.).reshape(5,4,3) + a0 = np.arange(60.).reshape(5,4,3) a1 = ctf.astensor(a0) return a0, a1 class KnowValues(unittest.TestCase): def test__getitem__(self): - a0 = numpy.arange(12.).reshape(4,3) + a0 = np.arange(12.).reshape(4,3) a1 = ctf.astensor(a0) self.assertTrue(allclose(a1[3], a0[3])) self.assertEqual(a1[(3,1)], a1[3,1]) @@ -32,21 +32,21 @@ def test__getitem__(self): with self.assertRaises(IndexError): a1[[3,4]] - a0 = numpy.arange(60.).reshape(5,4,3) + a0 = np.arange(60.).reshape(5,4,3) a1 = ctf.astensor(a0) self.assertTrue(allclose(a1[1:3,:,2:], a0[1:3,:,2:])) # def test_fancyindex(self): -# a0 = numpy.arange(60.).reshape(5,4,3) +# a0 = np.arange(60.).reshape(5,4,3) # a1 = ctf.astensor(a0) -# idx = numpy.arange(3) +# idx = np.arange(3) # self.assertTrue(a1[idx,idx,:].shape == (3, 3)) # self.assertTrue(a1[:,idx,idx].shape == (5, 3)) # self.assertTrue(a1[idx,:,idx].shape == (3, 4)) # self.assertTrue(allclose(a1[idx,idx+1,:], a0[idx,idx+1,:])) def test__getitem__(self): - a0 = numpy.arange(12.).reshape(4,3) + a0 = np.arange(12.).reshape(4,3) a1 = ctf.astensor(a0) self.assertTrue(a1.shape == (4,3)) self.assertTrue(a1[1].shape == (3,)) @@ -159,8 +159,8 @@ def test__setitem__(self): # Some advanced fancy indices which involve multiple dimensions of a tensor. # Remove these tests if they are not compatible to the distributed tensor # structure. - #idx = numpy.array([1,2,3]) - #idy = numpy.array([0,2]) + #idx = np.array([1,2,3]) + #idy = np.array([0,2]) #a0, a1 = a0_and_a1() #a1[idx[:,None],idy] = 99 #a0[idx[:,None],idy] = 99 @@ -178,9 +178,9 @@ def test__setitem__(self): #a0[idx[:,None,None],idy[:,None],idy] = 99 #self.assertTrue(allclose(a1, a0)) - #bidx = numpy.zeros(5, dtype=bool) - #bidy = numpy.zeros(4, dtype=bool) - #bidz = numpy.zeros(3, dtype=bool) + #bidx = np.zeros(5, dtype=bool) + #bidy = np.zeros(4, dtype=bool) + #bidz = np.zeros(3, dtype=bool) #bidx[idx] = True #bidy[idy] = True #bidz[idy] = True @@ -215,11 +215,11 @@ def test__setitem__(self): def test__getslice__(self): - a0 = ctf.astensor(numpy.arange(12.).reshape(4,3)) + a0 = ctf.astensor(np.arange(12.).reshape(4,3)) self.assertTrue(a0[1:].shape == (3,3)) def test__setslice__(self): - a0 = ctf.astensor(numpy.arange(12.).reshape(4,3)) + a0 = ctf.astensor(np.arange(12.).reshape(4,3)) a0[1:3] = 9 def test_slice_sym_4d(self): @@ -287,7 +287,7 @@ def test_noslice_sym_3d(self): self.assertTrue(allclose(ref, dat)) def run_tests(): - numpy.random.seed(5330); + np.random.seed(5330); wrld = ctf.comm() if ctf.comm().rank() != 0: result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(KnowValues))) diff --git a/test/python/test_partition.py b/test/python/test_partition.py index b815b3f0..d15936d8 100644 --- a/test/python/test_partition.py +++ b/test/python/test_partition.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import unittest -import numpy +import numpy as np import ctf import os import sys @@ -23,7 +23,7 @@ def test_partition(self): self.assertTrue(allclose(BB,CC)) def run_tests(): - numpy.random.seed(5330); + np.random.seed(5330); wrld = ctf.comm() if ctf.comm().rank() != 0: result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(KnowValues))) diff --git a/test/python/test_sparse.py b/test/python/test_sparse.py index e371290d..3a74c9e2 100644 --- a/test/python/test_sparse.py +++ b/test/python/test_sparse.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import unittest -import numpy +import numpy as np import ctf import os import sys @@ -21,10 +21,10 @@ def test_einsum_hadamard(self): c1.fill_sp_random(0., 1., 0.1) d1 = ctf.einsum('ijk,jkl->ijkl', a1, b1) - e1 = numpy.einsum('ijk,jkl->ijkl', ctf.to_nparray(a1), ctf.to_nparray(b1)) + e1 = np.einsum('ijk,jkl->ijkl', ctf.to_nparray(a1), ctf.to_nparray(b1)) self.assertTrue(allclose(d1,e1)) d2 = ctf.einsum('ijk,jkl->ijkl', a1, c1) - e2 = numpy.einsum('ijk,jkl->ijkl', ctf.to_nparray(a1), ctf.to_nparray(c1)) + e2 = np.einsum('ijk,jkl->ijkl', ctf.to_nparray(a1), ctf.to_nparray(c1)) self.assertTrue(allclose(d2,e2)) def test_scaled_expression(self): @@ -51,18 +51,22 @@ def test_scaled_expression(self): b_np = ctf.to_nparray(b_dn) c_np = ctf.to_nparray(c_dn) - c_sp.i("ijk") << 2.3*a_sp.i("ijl")*b_sp.i("kjl") + 7*c_sp.i("ijk") - a_sp.i("ijk") - 1. * a_sp.i("ijk") - 2 * b_sp.i("ijk") - c_dn.i("ijk") << 2.3*a_dn.i("ijl")*b_dn.i("kjl") + 7*c_dn.i("ijk") - a_dn.i("ijk") - 1. * a_dn.i("ijk") - 2 * b_dn.i("ijk") - c_np += 2.3*numpy.einsum("ijl,kjl->ijk",a_np,b_np) + 7*c_np - a_np - 1. * a_np - 2 * b_np + c_sp.i("ijk") << a_sp.i("ijl")*b_sp.i("kjl")*2.3 + c_sp.i("ijk")*7 - a_sp.i("ijk") - a_sp.i("ijk") - b_sp.i("ijk")*2 + c_dn.i("ijk") << a_dn.i("ijl")*b_dn.i("kjl")*2.3 + c_dn.i("ijk")*7 - a_dn.i("ijk") - a_dn.i("ijk") - b_dn.i("ijk")*2 + c_np += 2.3*np.einsum("ijl,kjl->ijk",a_np,b_np) + 7*c_np - a_np - 1. * a_np - 2 * b_np self.assertTrue(allclose(c_np,c_dn)) self.assertTrue(allclose(c_np,c_sp)) def test_complex(self): - a0 = numpy.arange(27.).reshape(3,3,3) - b0 = numpy.arange(27.).reshape(3,3,3) + a0 = np.arange(27.).reshape(3,3,3) + b0 = np.arange(27.).reshape(3,3,3) a1 = ctf.astensor(a0) b1 = ctf.astensor(b0) - .2*b1.i("ijk") << .7*a1.i("kij") + b1i = b1.i("ijk") + b1i.scale(.2) + a1i = b1.i("kij") + a1i.scale(.7) + b1i << a1i b0 = .2*b0 + .7*a0.transpose([1,2,0]) self.assertTrue(allclose(b0,b1)) @@ -96,7 +100,7 @@ def test_sparse_SY(self): self.assertTrue(allclose(ctf.vecnorm(X),ctf.vecnorm(Y))) def run_tests(): - numpy.random.seed(5330); + np.random.seed(5330); wrld = ctf.comm() if ctf.comm().rank() != 0: result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(KnowValues))) diff --git a/test/python/test_ufunc.py b/test/python/test_ufunc.py index a5a823b7..78129b7e 100644 --- a/test/python/test_ufunc.py +++ b/test/python/test_ufunc.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import unittest -import numpy +import numpy as np import ctf import os import sys @@ -12,19 +12,19 @@ def allclose(a, b): class KnowValues(unittest.TestCase): def test_abs(self): - a0 = numpy.arange(2., 5.) + a0 = np.arange(2., 5.) a1 = ctf.from_nparray(a0) self.assertTrue(ctf.all(ctf.abs(a1) == ctf.abs(a0))) - self.assertTrue(ctf.all(ctf.abs(a1) == numpy.abs(a0))) + self.assertTrue(ctf.all(ctf.abs(a1) == np.abs(a0))) try: a1 = a1 + 1j - self.assertAlmostEqual(ctf.abs(a1).sum(), numpy.abs(a1.to_nparray()).sum(), 14) + self.assertAlmostEqual(ctf.abs(a1).sum(), np.abs(a1.to_nparray()).sum(), 14) except AttributeError: pass def test_eq(self): - a0 = numpy.arange(6).reshape(2,3) + a0 = np.arange(6).reshape(2,3) a1 = ctf.array(a0) a2 = ctf.array(a0) self.assertTrue(ctf.all(a1==a2)) @@ -34,28 +34,28 @@ def test_eq(self): def test_conj(self): a0 = ctf.zeros((2,3)) - self.assertTrue(ctf.conj(a0).dtype == numpy.double) - self.assertTrue(a0.conj().dtype == numpy.double) + self.assertTrue(ctf.conj(a0).dtype == np.double) + self.assertTrue(a0.conj().dtype == np.double) - a0 = ctf.zeros((2,3), dtype=numpy.complex) - self.assertTrue(ctf.conj(a0).dtype == numpy.complex128) - self.assertTrue(a0.conj().dtype == numpy.complex128) + a0 = ctf.zeros((2,3), dtype=complex) + self.assertTrue(ctf.conj(a0).dtype == np.complex128) + self.assertTrue(a0.conj().dtype == np.complex128) a0[:] = 1j a0 = a0.conj() self.assertTrue(ctf.all(a0 == -1j)) def test__mul__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1*.5, a0*.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(a1*a2, a0*(a0*.2+1j))) self.assertTrue(allclose(a1*a0, a0*a0)) - a2 = numpy.arange(6.).reshape(3,2) + a2 = np.arange(6.).reshape(3,2) self.assertTrue(allclose(a1*a2, a0*a2)) - a0 = ctf.astensor(numpy.arange(4.)) - a1 = ctf.astensor(numpy.arange(3.)) + a0 = ctf.astensor(np.arange(4.)) + a1 = ctf.astensor(np.arange(3.)) self.assertTrue((a0.reshape(4,1)*a1).shape == (4,3)) self.assertTrue((a1*a0.reshape(4,1)).shape == (4,3)) self.assertTrue((a1.reshape(1,3)*a0.reshape(4,1)).shape == (4,3)) @@ -63,16 +63,16 @@ def test__mul__(self): self.assertTrue((a1.reshape(1,1,3)*a0.reshape(4,1,1)).shape == (4,1,3)) def test__add__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1+.5, a0+.5)) a2 = ctf.astensor(a0*.2+1j) self.assertTrue(allclose(a1+a2, a0+(a0*.2+1j))) self.assertTrue(allclose(a1+a0, a0+a0)) - a2 = numpy.arange(6.).reshape(3,2) + a2 = np.arange(6.).reshape(3,2) self.assertTrue(allclose(a1+a2, a0+a2)) - a0 = ctf.astensor(numpy.arange(4.)) - a1 = ctf.astensor(numpy.arange(3.)) + a0 = ctf.astensor(np.arange(4.)) + a1 = ctf.astensor(np.arange(3.)) self.assertTrue((a0.reshape(4,1)+a1).shape == (4,3)) self.assertTrue((a1+a0.reshape(4,1)).shape == (4,3)) self.assertTrue((a1.reshape(1,3)+a0.reshape(4,1)).shape == (4,3)) @@ -81,7 +81,7 @@ def test__add__(self): self.assertTrue((a1.reshape(1,1,3)+a0.reshape(4,1,1)).shape == (4,1,3)) def test__sub__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1-.5, a0-.5)) a2 = ctf.astensor(a0*.2+1j) @@ -89,7 +89,7 @@ def test__sub__(self): self.assertTrue(allclose(a1-a0, a0-a0)) def test__div__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1/.5, a0/.5)) a2 = ctf.astensor(a0*.2+1j) @@ -97,7 +97,7 @@ def test__div__(self): self.assertTrue(allclose(a1/a0, a0/a0)) def test_power(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(ctf.power(a1, .5), a0**.5)) a2 = ctf.astensor(a0*.2+1j) @@ -105,7 +105,7 @@ def test_power(self): self.assertTrue(allclose(ctf.power(a1, a0), a0**a0)) def test__pow__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0) self.assertTrue(allclose(a1**.5, a0**.5)) a2 = ctf.astensor(a0*.2+1j) @@ -113,88 +113,88 @@ def test__pow__(self): self.assertTrue(allclose(a1**a0, a0**a0)) def test__imul__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 *= .5 self.assertTrue(allclose(a1, a0*.5)) - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() with self.assertRaises(TypeError): a1 *= a0*.2+1j - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() - a2 = numpy.arange(6.).reshape(3,2) + a2 = np.arange(6.).reshape(3,2) a1 *= a2 self.assertTrue(allclose(a1, a0*a2)) - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() - a2 = numpy.arange(2.) + a2 = np.arange(2.) a1 *= a2 self.assertTrue(allclose(a1, a0*a2)) def test__iadd__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 += .5 self.assertTrue(allclose(a1, a0+.5)) - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() with self.assertRaises(TypeError): a1 += a0*.2+1j - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() - a2 = numpy.arange(6.).reshape(3,2) + a2 = np.arange(6.).reshape(3,2) a1 += a2 self.assertTrue(allclose(a1, a0+a2)) - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() - a2 = numpy.arange(2.) + a2 = np.arange(2.) a1 += a2 self.assertTrue(allclose(a1, a0+a2)) def test__isub__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 -= .5 self.assertTrue(allclose(a1, a0-.5)) def test__idiv__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 /= .5 self.assertTrue(allclose(a1, a0/.5)) def test__ipow__(self): - a0 = numpy.arange(24.).reshape(4,3,2) + .4 + a0 = np.arange(24.).reshape(4,3,2) + .4 a1 = ctf.astensor(a0).copy() a1 **= .5 self.assertTrue(allclose(a1, a0**.5)) def test_set_item(self): - a0 = numpy.arange(24.).reshape(4,3,2) + 400. - b0 = numpy.arange(6.).reshape(3,2) + a0 = np.arange(24.).reshape(4,3,2) + 400. + b0 = np.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() a0[:] = b0 a1[:] = b1 self.assertTrue(allclose(a1, a0)) - a0 = numpy.arange(24.).reshape(4,3,2) + 400. - b0 = numpy.arange(6.).reshape(3,2) + a0 = np.arange(24.).reshape(4,3,2) + 400. + b0 = np.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() a0[1:,1] = b0 a1[1:,1] = b1 self.assertTrue(allclose(a1, a0)) - a0 = numpy.arange(24.).reshape(4,3,2) + 400. - b0 = numpy.arange(6.).reshape(3,2) + a0 = np.arange(24.).reshape(4,3,2) + 400. + b0 = np.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() a0[2:,:,1] = b0[:,1] @@ -202,24 +202,24 @@ def test_set_item(self): self.assertTrue(allclose(a1, a0)) def test_get_item(self): - a0 = numpy.arange(24.).reshape(4,3,2) + 400. - b0 = numpy.arange(6.).reshape(3,2) + a0 = np.arange(24.).reshape(4,3,2) + 400. + b0 = np.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() b0 = a0[:] b1 = a1[:] self.assertTrue(allclose(b1, b0)) - a0 = numpy.arange(24.).reshape(4,3,2) + 400. - b0 = numpy.arange(6.).reshape(3,2) + a0 = np.arange(24.).reshape(4,3,2) + 400. + b0 = np.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() b0 = a0[1:,1] b1 = a1[1:,1] self.assertTrue(allclose(b1, b0)) - a0 = numpy.arange(24.).reshape(4,3,2) + 400. - b0 = numpy.arange(6.).reshape(3,2) + a0 = np.arange(24.).reshape(4,3,2) + 400. + b0 = np.arange(6.).reshape(3,2) a1 = ctf.astensor(a0).copy() b1 = ctf.astensor(b0).copy() b0[:,1] = a0[2,:,1] @@ -228,7 +228,7 @@ def test_get_item(self): def run_tests(): - numpy.random.seed(5330); + np.random.seed(5330); wrld = ctf.comm() if ctf.comm().rank() != 0: result = unittest.TextTestRunner(stream = open(os.devnull, 'w')).run(unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(KnowValues)))