Skip to content

Commit

Permalink
fixes to make new version of cython (3.0+) happy
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonik committed Nov 8, 2023
1 parent f1ec874 commit 80dc724
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 123 deletions.
24 changes: 24 additions & 0 deletions src/interface/set.h
Expand Up @@ -663,6 +663,30 @@ namespace CTF {
std::sort((dtypePair<dtype>*)pairs,((dtypePair<dtype>*)pairs)+n);
}

void to_s_of_a(int64_t n, char const * pairs, int64_t * keys, char * vals) const {
Pair<dtype> const * dpairs = (Pair<dtype> const *)pairs;
dtype * dvals = (dtype*)vals;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int64_t i=0; i<n; i++){
keys[i] = dpairs[i].k;
dvals[i] = dpairs[i].d;
}
}

void to_a_of_s(int64_t n, int64_t const * keys, char const * vals, char * pairs) const {
Pair<dtype> * dpairs = (Pair<dtype>*)pairs;
dtype const * dvals = (dtype const*)vals;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int64_t i=0; i<n; i++){
dpairs[i].k = keys[i];
dpairs[i].d = dvals[i];
}
}

void copy(char * a, char const * b) const {
((dtype *)a)[0] = ((dtype const *)b)[0];
}
Expand Down
51 changes: 23 additions & 28 deletions src/interface/tensor.cxx
Expand Up @@ -360,15 +360,7 @@ namespace CTF {
*data = (dtype*)CTF_int::alloc((*npair)*sizeof(dtype));
CTF_int::memprof_dealloc(*global_idx);
CTF_int::memprof_dealloc(*data);
CTF_int::PairIterator pairs(sr, cpairs);

#ifdef _OPENMP
#pragma omp parallel for
#endif
for (i=0; i<(*npair); i++){
(*global_idx)[i] = pairs[i].k();
pairs[i].read_val((char*)((*data)+i));
}
sr->to_s_of_a(*npair, cpairs, *global_idx, (char*)*data);
if (cpairs != NULL) sr->pair_dealloc(cpairs);
}

Expand Down Expand Up @@ -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; i<npair; i++){
pairs[i].k = global_idx[i];
pairs[i].d = data[i];
}
sr->to_a_of_s(npair, global_idx, (char*)data, (char*)pairs);
//#ifdef _OPENMP
// #pragma omp parallel for
//#endif
// for (i=0; i<npair; i++){
// pairs[i].k = global_idx[i];
// pairs[i].d = data[i];
// }
ret = CTF_int::tensor::read(npair, cpairs);
if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read\n"); IASSERT(0); return; }
for (i=0; i<npair; i++){
Expand Down Expand Up @@ -463,16 +456,17 @@ namespace CTF {
int64_t const * global_idx,
dtype const * data) {
int ret;
int64_t i;
//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; i<npair; i++){
pairs[i].k = global_idx[i];
pairs[i].d = data[i];
}
sr->to_a_of_s(npair, global_idx, (char*)data, (char*)pairs);
//#ifdef _OPENMP
// #pragma omp parallel for
//#endif
// for (i=0; i<npair; i++){
// pairs[i].k = global_idx[i];
// pairs[i].d = data[i];
// }
/*char * cpairs = sr->pair_alloc(npair);
CTF_int::PairIterator pairs = CTF_int::PairIterator(sr, cpairs);
for (i=0; i<npair; i++){
Expand Down Expand Up @@ -721,10 +715,11 @@ namespace CTF {
int64_t i;
char * cpairs = sr->pair_alloc(npair);
Pair< dtype > * pairs =(Pair< dtype >*)cpairs;
for (i=0; i<npair; i++){
pairs[i].k = global_idx[i];
pairs[i].d = data[i];
}
sr->to_a_of_s(npair, global_idx, (char*)data, (char*)pairs);
//for (i=0; i<npair; i++){
// pairs[i].k = global_idx[i];
// pairs[i].d = data[i];
//}
ret = CTF_int::tensor::read(npair, (char*)&alpha, (char*)&beta, cpairs);
if (ret != CTF_int::SUCCESS){ printf("CTF ERROR: failed to execute function read\n"); IASSERT(0); return; }
for (i=0; i<npair; i++){
Expand Down
62 changes: 42 additions & 20 deletions src/tensor/algstrct.cxx
Expand Up @@ -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; i<n; i++){
keys[i] = rA[i].k();
rA[i].read_val(vals + i*el_size);
}
}

void algstrct::to_a_of_s(int64_t n, int64_t const * keys, char const * vals, char * pairs) const {
PairIterator wA(this, pairs);
#ifdef _OPENMP
#pragma omp parallel for
#endif
for (int64_t i=0; i<n; i++){
wA[i].write_key(keys[i]);
wA[i].write_val(vals + i*el_size);
}
}

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; i<n; i++){
int64_t k = rA[i].k();
int64_t k_new = 0;
for (int j=0; j<order; j++){
k_new += (k%old_lens[j])*new_lda[j];
k = k/old_lens[j];
}
((int64_t*)wA[i].ptr)[0] = k_new;
wA[i].write_val(rA[i].d());
//printf("value %lf old key %ld new key %ld\n",((double*)wA[i].d())[0], rA[i].k(), wA[i].k());
}


}

void algstrct::scal(int n,
char const * alpha,
char * X,
Expand Down Expand Up @@ -918,26 +960,6 @@ namespace CTF_int {
sr->sort(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; i<n; i++){
int64_t k = rA[i].k();
int64_t k_new = 0;
for (int j=0; j<order; j++){
k_new += (k%old_lens[j])*new_lda[j];
k = k/old_lens[j];
}
((int64_t*)wA[i].ptr)[0] = k_new;
wA[i].write_val(rA[i].d());
//printf("value %lf old key %ld new key %ld\n",((double*)wA[i].d())[0], rA[i].k(), wA[i].k());
}


}

void ConstPairIterator::pin(int64_t n, int order, int64_t const * lens, int const * divisor, PairIterator pi_new){
TAU_FSTART(pin);
ConstPairIterator pi = *this;
Expand Down
19 changes: 19 additions & 0 deletions src/tensor/algstrct.h
Expand Up @@ -339,6 +339,25 @@ namespace CTF_int {
* \brief sorts n sets of pairs using std::sort
*/
virtual void sort(int64_t n, char * pairs) const;

/**
* \brief converts pair array into struct of arrays
* \param[in] n number of key, value pairs to extract
* \param[in] pairs to convert
* \param[in,out] keys array of keys
* \param[in,out] vals array of values
*/
virtual void to_s_of_a(int64_t n, char const * pairs, int64_t * keys, char * vals) const;

/**
* \brief converts pair array into array of structs
* \param[in] n number of key, value pairs to extract
* \param[in] keys array of keys
* \param[in] vals array of values
* \param[in,out] pairs to convert
*/
virtual void to_a_of_s(int64_t n, int64_t const * keys, char const * vals, char * pairs) const;


/** estimate time in seconds necessary for CSR reduction with input of size msg_sz */
double estimate_csr_red_time(int64_t msg_sz, CommData const * cdt) const;
Expand Down
7 changes: 7 additions & 0 deletions src/tensor/untyped_tensor.cxx
Expand Up @@ -2405,6 +2405,13 @@ namespace CTF_int {
return ipr.ptr;
}

void tensor::read_all_pairs(int64_t * num_pair, bool unpack, int64_t ** inds, char ** vals, bool nonzero_only) const {
char * pairs = read_all_pairs(num_pair, unpack, nonzero_only);
*inds = (int64_t*)malloc(sizeof(int64_t)*(*num_pair));
*vals = (char*)malloc(sr->el_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;
Expand Down
12 changes: 12 additions & 0 deletions src/tensor/untyped_tensor.h
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions 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)

42 changes: 42 additions & 0 deletions 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
Expand Down Expand Up @@ -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(<double[:n]><double*>cdata)
elif dtype == np.float32:
return np.asarray(<float[:n]><float*>cdata)
elif dtype == np.complex64:
return np.asarray(<complex64_t[:n]><complex64_t*>cdata)
elif dtype == np.complex128:
return np.asarray(<complex128_t[:n]><complex128_t*>cdata)
elif dtype == np.int64:
return np.asarray(<iint64_t[:n]><iint64_t*>cdata)
elif dtype == np.int32:
return np.asarray(<int[:n]><int*>cdata)
elif dtype == np.bool_:
return np.asarray(<bool[:n]><bool*>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 = <complex128_t[:n]>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 = <int64_t[:n]>cdata
# data = np.empty(n, dtype=np.int64)
# data[:] = cview[:]
# return data


6 changes: 3 additions & 3 deletions 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
Expand Down
4 changes: 2 additions & 2 deletions 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)
Expand Down
2 changes: 1 addition & 1 deletion 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:
"""
Expand Down
5 changes: 3 additions & 2 deletions 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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 80dc724

Please sign in to comment.