<commit_before>/*
 * Copyright 2016 Open Connectome Project (http://openconnecto.me)
 * Written by <NAME> (<EMAIL>)
 *
 * This file is part of FlashMatrix.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "project_matrix_store.h"
#include "dense_matrix.h"

namespace fm
{

namespace detail
{

sparse_project_matrix_store::sparse_project_matrix_store(size_t nrow,
		size_t ncol, matrix_layout_t layout, const scalar_type &type): mem_matrix_store(
			nrow, ncol, type)
{
	this->layout = layout;
}

namespace
{

template<class T>
class rand_init: public type_set_operate<T>
{
public:
	virtual void set(T *arr, size_t num_eles, off_t row_idx,
			off_t col_idx) const {
		for (size_t i = 0; i < num_eles; i++) {
			if (random() % 2 == 0)
				arr[i] = 1;
			else
				arr[i] = -1;
		}
	}
};

}

sparse_project_matrix_store::ptr sparse_project_matrix_store::create_sparse_rand(
		size_t nrow, size_t ncol, matrix_layout_t layout,
		const scalar_type &type, double density)
{
	if (type != get_scalar_type<double>() && type != get_scalar_type<int>()) {
		BOOST_LOG_TRIVIAL(error)
			<< "sparse project matrix only supports int and double";
		return sparse_project_matrix_store::ptr();
	}

	sparse_project_matrix_store::ptr ret(new sparse_project_matrix_store(nrow,
				ncol, layout, type));
	// I intentially increase the density to generate the number of nnz.
	// It's possible I'll pick the same slot twice in the for loop below.
	size_t required = density * nrow * ncol;
	ret->nnz_idxs.resize((density * 1.1) * nrow * ncol);
	for (size_t i = 0; i < ret->nnz_idxs.size(); i++) {
		ret->nnz_idxs[i].row_idx = random() % nrow;
		ret->nnz_idxs[i].col_idx = random() % ncol;
	}
	if (layout == matrix_layout_t::L_ROW && ret->is_wide())
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				wide_row_first_comp());
	else if (ret->is_wide())
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				wide_col_first_comp());
	else if (layout == matrix_layout_t::L_ROW)
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				tall_row_first_comp());
	else
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				tall_col_first_comp());

	// There might be duplicates in the vector. We should remove them.
	auto end = std::unique(ret->nnz_idxs.begin(), ret->nnz_idxs.end());
	if (end - ret->nnz_idxs.begin() > (off_t) required)
		ret->nnz_idxs.resize(required);

	mem_col_matrix_store::ptr vals = mem_col_matrix_store::create(
			ret->nnz_idxs.size(), 1, type);
	if (type == get_scalar_type<double>())
		vals->set_data(rand_init<double>());
	else if (type == get_scalar_type<int>())
		vals->set_data(rand_init<int>());
	ret->vals = vals;
	return ret;
}

const char *sparse_project_matrix_store::get(size_t row, size_t col) const
{
	throw unsupported_exception(
			"don't support getting an entry in a sparse matrix");
}

matrix_store::const_ptr sparse_project_matrix_store::transpose() const
{
	matrix_layout_t new_layout;
	if (store_layout() == matrix_layout_t::L_COL)
		new_layout = matrix_layout_t::L_ROW;
	else
		new_layout = matrix_layout_t::L_COL;

	sparse_project_matrix_store *ret = new sparse_project_matrix_store(
			get_num_cols(), get_num_rows(), new_layout, get_type());
	ret->nnz_idxs.resize(nnz_idxs.size());
	for (size_t i = 0; i < nnz_idxs.size(); i++) {
		ret->nnz_idxs[i].row_idx = nnz_idxs[i].col_idx;
		ret->nnz_idxs[i].col_idx = nnz_idxs[i].row_idx;
	}
	ret->vals = vals;
	return matrix_store::const_ptr(ret);
}

local_matrix_store::const_ptr sparse_project_matrix_store::get_portion(
		size_t start_row, size_t start_col, size_t num_rows, size_t num_cols) const
{
	// TODO we current only support get the entire portion.
	if (is_wide() && (start_row > 0 || num_rows != get_num_rows())) {
		BOOST_LOG_TRIVIAL(error) << "we only support getting the entire portion";
		return local_matrix_store::const_ptr();
	}
	else if (!is_wide() && (start_col > 0 || num_cols != get_num_cols())) {
		BOOST_LOG_TRIVIAL(error) << "we only support getting the entire portion";
		return local_matrix_store::const_ptr();
	}

	// The requested portion is in the same physical portion.
	if (is_wide() && start_col / mem_matrix_store::CHUNK_SIZE
			!= (start_col + num_cols - 1) / mem_matrix_store::CHUNK_SIZE) {
		BOOST_LOG_TRIVIAL(error)
			<< "the requested portion should be in the same physical portion";
		return local_matrix_store::const_ptr();
	}
	else if (!is_wide() && start_row / mem_matrix_store::CHUNK_SIZE
			!= (start_row + num_rows - 1) / mem_matrix_store::CHUNK_SIZE) {
		BOOST_LOG_TRIVIAL(error)
			<< "the requested portion should be in the same physical portion";
		return local_matrix_store::const_ptr();
	}

	if (start_row + num_rows > get_num_rows()
			|| start_col + num_cols > get_num_cols()) {
		BOOST_LOG_TRIVIAL(error) << "out of boundary";
		return local_matrix_store::const_ptr();
	}

	// Find the starting and ending points in the vector of offsets.
	// Search for [start, end]
	sparse_project_matrix_store::nnz_idx start, end;
	start.row_idx = start_row;
	start.col_idx = start_col;
	end.row_idx = start_row + num_rows - 1;
	end.col_idx = start_col + num_cols - 1;
	std::vector<nnz_idx>::const_iterator start_it, end_it;
	if (layout == matrix_layout_t::L_ROW && is_wide()) {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				wide_row_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				wide_row_first_comp());
	}
	else if (is_wide()) {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				wide_col_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				wide_col_first_comp());
	}
	else if (layout == matrix_layout_t::L_ROW) {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				tall_row_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				tall_row_first_comp());
	}
	else {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				tall_col_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				tall_col_first_comp());
	}

	// There isn't nnz in the portion.
	if (start_it == nnz_idxs.end() || start_it == end_it)
		return local_matrix_store::const_ptr();

	// We search for [start, end].
	// If the last element has value `end', we need to move the end iterator
	// to include the last element.
	if (end_it != nnz_idxs.end()<commit_msg>[Matrix]: return an empty local sparse matrix store if no nz values.<commit_after>/*
 * Copyright 2016 Open Connectome Project (http://openconnecto.me)
 * Written by <NAME> (<EMAIL>)
 *
 * This file is part of FlashMatrix.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "project_matrix_store.h"
#include "dense_matrix.h"

namespace fm
{

namespace detail
{

sparse_project_matrix_store::sparse_project_matrix_store(size_t nrow,
		size_t ncol, matrix_layout_t layout, const scalar_type &type): mem_matrix_store(
			nrow, ncol, type)
{
	this->layout = layout;
}

namespace
{

template<class T>
class rand_init: public type_set_operate<T>
{
public:
	virtual void set(T *arr, size_t num_eles, off_t row_idx,
			off_t col_idx) const {
		for (size_t i = 0; i < num_eles; i++) {
			if (random() % 2 == 0)
				arr[i] = 1;
			else
				arr[i] = -1;
		}
	}
};

}

sparse_project_matrix_store::ptr sparse_project_matrix_store::create_sparse_rand(
		size_t nrow, size_t ncol, matrix_layout_t layout,
		const scalar_type &type, double density)
{
	if (type != get_scalar_type<double>() && type != get_scalar_type<int>()) {
		BOOST_LOG_TRIVIAL(error)
			<< "sparse project matrix only supports int and double";
		return sparse_project_matrix_store::ptr();
	}

	sparse_project_matrix_store::ptr ret(new sparse_project_matrix_store(nrow,
				ncol, layout, type));
	// I intentially increase the density to generate the number of nnz.
	// It's possible I'll pick the same slot twice in the for loop below.
	size_t required = density * nrow * ncol;
	ret->nnz_idxs.resize((density * 1.1) * nrow * ncol);
	for (size_t i = 0; i < ret->nnz_idxs.size(); i++) {
		ret->nnz_idxs[i].row_idx = random() % nrow;
		ret->nnz_idxs[i].col_idx = random() % ncol;
	}
	if (layout == matrix_layout_t::L_ROW && ret->is_wide())
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				wide_row_first_comp());
	else if (ret->is_wide())
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				wide_col_first_comp());
	else if (layout == matrix_layout_t::L_ROW)
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				tall_row_first_comp());
	else
		std::sort(ret->nnz_idxs.begin(), ret->nnz_idxs.end(),
				tall_col_first_comp());

	// There might be duplicates in the vector. We should remove them.
	auto end = std::unique(ret->nnz_idxs.begin(), ret->nnz_idxs.end());
	if (end - ret->nnz_idxs.begin() > (off_t) required)
		ret->nnz_idxs.resize(required);

	mem_col_matrix_store::ptr vals = mem_col_matrix_store::create(
			ret->nnz_idxs.size(), 1, type);
	if (type == get_scalar_type<double>())
		vals->set_data(rand_init<double>());
	else if (type == get_scalar_type<int>())
		vals->set_data(rand_init<int>());
	ret->vals = vals;
	return ret;
}

const char *sparse_project_matrix_store::get(size_t row, size_t col) const
{
	throw unsupported_exception(
			"don't support getting an entry in a sparse matrix");
}

matrix_store::const_ptr sparse_project_matrix_store::transpose() const
{
	matrix_layout_t new_layout;
	if (store_layout() == matrix_layout_t::L_COL)
		new_layout = matrix_layout_t::L_ROW;
	else
		new_layout = matrix_layout_t::L_COL;

	sparse_project_matrix_store *ret = new sparse_project_matrix_store(
			get_num_cols(), get_num_rows(), new_layout, get_type());
	ret->nnz_idxs.resize(nnz_idxs.size());
	for (size_t i = 0; i < nnz_idxs.size(); i++) {
		ret->nnz_idxs[i].row_idx = nnz_idxs[i].col_idx;
		ret->nnz_idxs[i].col_idx = nnz_idxs[i].row_idx;
	}
	ret->vals = vals;
	return matrix_store::const_ptr(ret);
}

local_matrix_store::const_ptr sparse_project_matrix_store::get_portion(
		size_t start_row, size_t start_col, size_t num_rows, size_t num_cols) const
{
	// TODO we current only support get the entire portion.
	if (is_wide() && (start_row > 0 || num_rows != get_num_rows())) {
		BOOST_LOG_TRIVIAL(error) << "we only support getting the entire portion";
		return local_matrix_store::const_ptr();
	}
	else if (!is_wide() && (start_col > 0 || num_cols != get_num_cols())) {
		BOOST_LOG_TRIVIAL(error) << "we only support getting the entire portion";
		return local_matrix_store::const_ptr();
	}

	// The requested portion is in the same physical portion.
	if (is_wide() && start_col / mem_matrix_store::CHUNK_SIZE
			!= (start_col + num_cols - 1) / mem_matrix_store::CHUNK_SIZE) {
		BOOST_LOG_TRIVIAL(error)
			<< "the requested portion should be in the same physical portion";
		return local_matrix_store::const_ptr();
	}
	else if (!is_wide() && start_row / mem_matrix_store::CHUNK_SIZE
			!= (start_row + num_rows - 1) / mem_matrix_store::CHUNK_SIZE) {
		BOOST_LOG_TRIVIAL(error)
			<< "the requested portion should be in the same physical portion";
		return local_matrix_store::const_ptr();
	}

	if (start_row + num_rows > get_num_rows()
			|| start_col + num_cols > get_num_cols()) {
		BOOST_LOG_TRIVIAL(error) << "out of boundary";
		return local_matrix_store::const_ptr();
	}

	// Find the starting and ending points in the vector of offsets.
	// Search for [start, end]
	sparse_project_matrix_store::nnz_idx start, end;
	start.row_idx = start_row;
	start.col_idx = start_col;
	end.row_idx = start_row + num_rows - 1;
	end.col_idx = start_col + num_cols - 1;
	std::vector<nnz_idx>::const_iterator start_it, end_it;
	if (layout == matrix_layout_t::L_ROW && is_wide()) {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				wide_row_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				wide_row_first_comp());
	}
	else if (is_wide()) {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				wide_col_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				wide_col_first_comp());
	}
	else if (layout == matrix_layout_t::L_ROW) {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				tall_row_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				tall_row_first_comp());
	}
	else {
		start_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), start,
				tall_col_first_comp());
		end_it = std::lower_bound(nnz_idxs.begin(), nnz_idxs.end(), end,
				tall_col_first_comp());
	}

	// There isn't nnz in the portion.
	if (start_it == nnz_idxs.end() || start_it == end_it) {
		std::vector<sparse_project_matrix_store::nnz_idx> empty_idxs;
		local_matrix_store::ptr empty_vals(new local_buf_col_matrix_store(
					0, 0, 0, 0, vals->get_type(), -1));
		return local_matrix_store::const_ptr(new local_sparse_matrix_store(
					start_row, start_col, num_rows, num_cols, empty_idxs,
					empty_vals, store_layout()));
	}

	// We search for [start, end].
	// If the last element has value `end', we need to move the end iterator
	// to include the last element.
	if (end_it != nnz_idxs.end()
{'fraction_non_alphanumeric': 0.13662667607621737, 'fraction_numerical': 0.0037402964008468598, 'mean_word_length': 4.841302555647156, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5876856', 'n_tokens_mistral': 5559, 'n_tokens_neox': 5217, 'n_words': 1221}