def __init__(self, A, lib, **kwargs): try: self.dense = isinstance(A, ndarray) and len(A.shape) == 2 self.CSC = isinstance(A, csc_matrix) self.CSR = isinstance(A, csr_matrix) assert self.dense or self.CSC or self.CSR assert A.dtype == c_float or A.dtype == c_double self.m = A.shape[0] self.n = A.shape[1] self.A = A self.lib = lib self.wDev = 0 self.double_precision = A.dtype == c_double self.settings = make_settings(self.double_precision, **kwargs) self.pysolution = Solution(self.double_precision, self.m, self.n) self.solution = make_solution(self.pysolution) self.info = make_info(self.double_precision) self.order = H2OConstants.ROW_MAJ if (self.CSR or self.dense) \ else H2OConstants.COL_MAJ if self.dense and not self.double_precision: self.work = self.lib.h2o4gpu_init_dense_single( self.wDev, self.order, self.m, self.n, cptr(A, c_float)) elif self.dense: self.work = self.lib.h2o4gpu_init_dense_double( self.wDev, self.order, self.m, self.n, cptr(A, c_double)) elif not self.double_precision: self.work = self.lib.h2o4gpu_init_sparse_single( self.wDev, self.order, self.m, self.n, A.nnz, cptr(A.data, c_float), cptr(A.indices, c_int), cptr(A.indptr, c_int)) else: self.work = self.lib.h2o4gpu_init_sparse_double( self.wDev, self.order, self.m, self.n, A.nnz, cptr(A.data, c_double), cptr(A.indices, c_int), cptr(A.indptr, c_int)) except AssertionError: print("Data must be a (m x n) numpy ndarray or scipy csc_matrix " "containing float32 or float64 values")
def _convert_to_ptr(data): """Convert data to a form which can be passed to C/C++ code. :param data: array_like :return: """ if data is not None: np_data, _, dtype = _to_np(data) if dtype == np.float32: c_ftype = c_float elif dtype == np.float64: c_ftype = c_double else: ValueError("No such dtype") data_ptr = cptr(np_data, dtype=c_ftype) else: data_ptr = None return data_ptr
def fit(self, f, g, **kwargs): try: # assert f,g types assert isinstance(f, FunctionVector) assert isinstance(g, FunctionVector) # assert f,g lengths assert f.length() == self.m assert g.length() == self.n # pass previous rho through, if not first run (rho=0) if self.info.rho > 0: self.settings.rho = self.info.rho # apply user inputs change_settings(self.settings, **kwargs) change_solution(self.pysolution, **kwargs) if not self.work: print("No viable H2O4GPU_work pointer to call solve()." "Call Solver.init( args... ) first") return elif not self.double_precision: self.lib.h2o4gpu_solve_single(self.work, pointer(self.settings), pointer(self.solution), pointer(self.info), cptr(f.a, c_float), cptr(f.b, c_float), cptr(f.c, c_float), cptr(f.d, c_float), cptr(f.e, c_float), cptr(f.h, c_int), cptr(g.a, c_float), cptr(g.b, c_float), cptr(g.c, c_float), cptr(g.d, c_float), cptr(g.e, c_float), cptr(g.h, c_int)) else: self.lib.h2o4gpu_solve_double(self.work, pointer(self.settings), pointer(self.solution), pointer(self.info), cptr(f.a, c_double), cptr(f.b, c_double), cptr(f.c, c_double), cptr(f.d, c_double), cptr(f.e, c_double), cptr(f.h, c_int), cptr(g.a, c_double), cptr(g.b, c_double), cptr(g.c, c_double), cptr(g.d, c_double), cptr(g.e, c_double), cptr(g.h, c_int)) except AssertionError: print("\nf and g must be objects of type FunctionVector with:") print(">length of f = m, # of rows in solver's data matrix A") print(">length of g = n, # of columns in solver's data matrix A")