<commit_before>#cython: boundscheck=False, initializedcheck=False, wraparound=False
"""
Copyright (c) 2015 <NAME> <<EMAIL>>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""

from cython.parallel import parallel, prange
cimport openmp

import numpy
cimport numpy

numpy.import_array()

from ...Utilities.Projector_Utilities cimport itype, dtype, atomic_add

cdef extern from "StackedExtendedProjector_cpp.cpp" nogil:
	void sparse_matvec(numpy.ndarray inp, numpy.ndarray outp, numpy.npy_intp zsize, numpy.ndarray dz, numpy.ndarray bounds, numpy.ndarray col, numpy.ndarray row, numpy.ndarray coeff, int threads)

DTYPES = (numpy.float32, numpy.float64, numpy.complex64, numpy.complex128)
ITYPES = (numpy.int16, numpy.int32, numpy.int64, numpy.uint16, numpy.uint32, numpy.uint64)

# def matvec(
# 		numpy.ndarray vec,
# 		numpy.ndarray res,
# 		numpy.npy_intp zsize,
# 		numpy.ndarray dz,
# 		numpy.ndarray bounds,
# 		numpy.ndarray col,
# 		numpy.ndarray row,
# 		numpy.ndarray coeff,
# 		int threads=0):
#
# 	assert res.dtype==vec.dtype and coeff.dtype==vec.dtype, "all data-containing arrays should have the same Datatype: vec: %s, res: %s, coeff: %s"%(res.dtype, vec.dtype, coeff.dtype)
# 	assert res.dtype in DTYPES, "type of data-arrays not acceptable: %s"%res.dtype
#
# 	assert dz.dtype==bounds.dtype and dz.dtype==col.dtype and dz.dtype==row.dtype, "all indices-containing arrays should have the same Datatype: idz: %s, bounds: %s, col: %s, row: %s"%(dz.dtype, bounds.dtype, col.dtype, row.dtype)
# 	assert dz.dtype in ITYPES, "type of indices-arrays not acceptable: %s"%dz.dtype
#
# 	if threads==0:
# 		threads = openmp.omp_get_max_threads()
#
# 	sparse_matvec(vec, res, zsize, dz, bounds, col, row, coeff, threads)

def matvec(
		numpy.ndarray[dtype, ndim=1] vec,
		numpy.ndarray[dtype, ndim=1] res,
		numpy.npy_intp zsize,
		numpy.ndarray[itype, ndim=1] dz,
		numpy.ndarray[itype, ndim=1] bounds,
		numpy.ndarray[itype, ndim=1] col,
		numpy.ndarray[itype, ndim=1] row,
		numpy.ndarray[dtype, ndim=1] coeff,
		int threads=0):

	cdef itype idz_size = dz.size
	cdef itype col_stride = numpy.round(vec.size/zsize)
	cdef itype row_stride = numpy.round(res.size/zsize)

	cdef dtype* vec_view
	cdef dtype* res_view
	cdef dtype* coeff_view
	cdef itype* idx_col_view
	cdef itype* idx_row_view

	if threads==0:
		threads = openmp.omp_get_max_threads()

	cdef itype i,j,jz,k

	with nogil, parallel(num_threads=threads):
		for i in prange(zsize, schedule='guided'):
			res_view = &res[i*row_stride]
			for j in range(idz_size):
				jz = i+dz[j]
				if jz<zsize and jz>=0:
					vec_view = &vec[jz*col_stride]
					coeff_view = &coeff[bounds[j]]
					idx_col_view = &col[bounds[j]]
					idx_row_view = &row[bounds[j]]

					for k in range(bounds[j+1]-bounds[j]):
						atomic_add(&res_view[idx_row_view[k]], coeff_view[k]*vec_view[idx_col_view[k]])<commit_msg>atomic add is not needed the way it is organized<commit_after>#cython: boundscheck=False, initializedcheck=False, wraparound=False
"""
Copyright (c) 2015 <NAME> <<EMAIL>>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""

from cython.parallel import parallel, prange
cimport openmp

import numpy
cimport numpy

numpy.import_array()

from ...Utilities.Projector_Utilities cimport itype, dtype, atomic_add

cdef extern from "StackedExtendedProjector_cpp.cpp" nogil:
	void sparse_matvec(numpy.ndarray inp, numpy.ndarray outp, numpy.npy_intp zsize, numpy.ndarray dz, numpy.ndarray bounds, numpy.ndarray col, numpy.ndarray row, numpy.ndarray coeff, int threads)

DTYPES = (numpy.float32, numpy.float64, numpy.complex64, numpy.complex128)
ITYPES = (numpy.int16, numpy.int32, numpy.int64, numpy.uint16, numpy.uint32, numpy.uint64)

# def matvec(
# 		numpy.ndarray vec,
# 		numpy.ndarray res,
# 		numpy.npy_intp zsize,
# 		numpy.ndarray dz,
# 		numpy.ndarray bounds,
# 		numpy.ndarray col,
# 		numpy.ndarray row,
# 		numpy.ndarray coeff,
# 		int threads=0):
#
# 	assert res.dtype==vec.dtype and coeff.dtype==vec.dtype, "all data-containing arrays should have the same Datatype: vec: %s, res: %s, coeff: %s"%(res.dtype, vec.dtype, coeff.dtype)
# 	assert res.dtype in DTYPES, "type of data-arrays not acceptable: %s"%res.dtype
#
# 	assert dz.dtype==bounds.dtype and dz.dtype==col.dtype and dz.dtype==row.dtype, "all indices-containing arrays should have the same Datatype: idz: %s, bounds: %s, col: %s, row: %s"%(dz.dtype, bounds.dtype, col.dtype, row.dtype)
# 	assert dz.dtype in ITYPES, "type of indices-arrays not acceptable: %s"%dz.dtype
#
# 	if threads==0:
# 		threads = openmp.omp_get_max_threads()
#
# 	sparse_matvec(vec, res, zsize, dz, bounds, col, row, coeff, threads)

def matvec(
		numpy.ndarray[dtype, ndim=1] vec,
		numpy.ndarray[dtype, ndim=1] res,
		numpy.npy_intp zsize,
		numpy.ndarray[itype, ndim=1] dz,
		numpy.ndarray[itype, ndim=1] bounds,
		numpy.ndarray[itype, ndim=1] col,
		numpy.ndarray[itype, ndim=1] row,
		numpy.ndarray[dtype, ndim=1] coeff,
		int threads=0):

	cdef itype idz_size = dz.size
	cdef itype col_stride = numpy.round(vec.size/zsize)
	cdef itype row_stride = numpy.round(res.size/zsize)

	cdef dtype* vec_view
	cdef dtype* res_view
	cdef dtype* coeff_view
	cdef itype* idx_col_view
	cdef itype* idx_row_view

	if threads==0:
		threads = openmp.omp_get_max_threads()

	cdef itype i,j,jz,k

	with nogil, parallel(num_threads=threads):
		for i in prange(zsize, schedule='guided'):
			res_view = &res[i*row_stride]
			for j in range(idz_size):
				jz = i+dz[j]
				if jz<zsize and jz>=0:
					vec_view = &vec[jz*col_stride]
					coeff_view = &coeff[bounds[j]]
					idx_col_view = &col[bounds[j]]
					idx_row_view = &row[bounds[j]]

					for k in range(bounds[j+1]-bounds[j]):
						res_view[idx_row_view[k]] += coeff_view[k]*vec_view[idx_col_view[k]]
{'fraction_non_alphanumeric': 0.1114269471290506, 'fraction_numerical': 0.010801591813530414, 'mean_word_length': 4.782251437962202, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4889614', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2599, 'n_words': 742}