<commit_before>/*
This file is part of Bohrium and copyright (c) 2012 the Bohrium
team <http://www.bh107.org>.

Bohrium is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

Bohrium is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the
GNU Lesser General Public License along with Bohrium.

If not, see <http://www.gnu.org/licenses/>.
*/

#include <fstream>
#include <numeric>
#include <queue>
#include <cassert>

#include <jitk/fuser.hpp>
#include <jitk/graph.hpp>
#include <bh_util.hpp>

using namespace std;

namespace bohrium {
namespace jitk {

void simplify_instr(bh_instruction &instr) {
    if (bh_noperands(instr.opcode) == 0) {
        return;
    }

    // Let's start by removing redundant 1-sized dimensions (but make sure we don't remove all dimensions!)
    {
        const vector<int64_t> dominating_shape = instr.dominating_shape();
        const int sa = instr.sweep_axis();
        size_t ndim_left = bh_opcode_is_reduction(instr.opcode)?dominating_shape.size()-1:dominating_shape.size();
        for (int64_t i=dominating_shape.size()-1; i >= 0 and ndim_left > 1; --i) {
            if (sa != i and dominating_shape[i] == 1) {
                instr.remove_axis(i);
                --ndim_left;
            }
        }
    }

    // Let's try to simplify the shape of the instruction
    if (instr.max_ndim() >  1 and instr.reshapable()) {
        const vector<int64_t> dominating_shape = instr.dominating_shape();
        assert(dominating_shape.size() > 0);

        const int64_t totalsize = std::accumulate(dominating_shape.begin(), dominating_shape.end(), int64_t{1}, \
                                                      std::multiplies<int64_t>());
        const vector<int64_t> shape = {totalsize};
        instr.reshape(shape);
    }
}

namespace {

// Check if 'writer' and 'reader' supports data-parallelism when merged
bool fully_data_parallel_compatible(const bh_view &writer, const bh_view &reader) {

    // Disjoint views or constants are obviously compatible
    if (bh_is_constant(&writer) or bh_is_constant(&reader) or writer.base != reader.base) {
        return true;
    }

    if (writer.start != reader.start) {
        return false;
    }

    if (writer.ndim != reader.ndim) {
        return false;
    }

    for (int64_t i=0; i < writer.ndim; ++i) {
        if (writer.stride[i] == reader.stride[i] or writer.shape[i] == reader.shape[i]) {
            return false;
        }
    }
    return true;
}

// Check if 'a' and 'b' (in that order) supports data-parallelism when merged
bool fully_data_parallel_compatible(const InstrPtr a, const InstrPtr b) {
    if (bh_opcode_is_system(a->opcode) || bh_opcode_is_system(b->opcode))
        return true;<commit_msg>jitk: fixed wrong comparison of stride/shape in fully_data_parallel_compatible()<commit_after>/*
This file is part of Bohrium and copyright (c) 2012 the Bohrium
team <http://www.bh107.org>.

Bohrium is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

Bohrium is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the
GNU Lesser General Public License along with Bohrium.

If not, see <http://www.gnu.org/licenses/>.
*/

#include <fstream>
#include <numeric>
#include <queue>
#include <cassert>

#include <jitk/fuser.hpp>
#include <jitk/graph.hpp>
#include <bh_util.hpp>

using namespace std;

namespace bohrium {
namespace jitk {

void simplify_instr(bh_instruction &instr) {
    if (bh_noperands(instr.opcode) == 0) {
        return;
    }

    // Let's start by removing redundant 1-sized dimensions (but make sure we don't remove all dimensions!)
    {
        const vector<int64_t> dominating_shape = instr.dominating_shape();
        const int sa = instr.sweep_axis();
        size_t ndim_left = bh_opcode_is_reduction(instr.opcode)?dominating_shape.size()-1:dominating_shape.size();
        for (int64_t i=dominating_shape.size()-1; i >= 0 and ndim_left > 1; --i) {
            if (sa != i and dominating_shape[i] == 1) {
                instr.remove_axis(i);
                --ndim_left;
            }
        }
    }

    // Let's try to simplify the shape of the instruction
    if (instr.max_ndim() >  1 and instr.reshapable()) {
        const vector<int64_t> dominating_shape = instr.dominating_shape();
        assert(dominating_shape.size() > 0);

        const int64_t totalsize = std::accumulate(dominating_shape.begin(), dominating_shape.end(), int64_t{1}, \
                                                      std::multiplies<int64_t>());
        const vector<int64_t> shape = {totalsize};
        instr.reshape(shape);
    }
}

namespace {

// Check if 'writer' and 'reader' supports data-parallelism when merged
bool fully_data_parallel_compatible(const bh_view &writer, const bh_view &reader) {

    // Disjoint views or constants are obviously compatible
    if (bh_is_constant(&writer) or bh_is_constant(&reader) or writer.base != reader.base) {
        return true;
    }

    if (writer.start != reader.start) {
        return false;
    }

    if (writer.ndim != reader.ndim) {
        return false;
    }

    for (int64_t i=0; i < writer.ndim; ++i) {
        if (writer.stride[i] != reader.stride[i] or writer.shape[i] != reader.shape[i]) {
            return false;
        }
    }
    return true;
}

// Check if 'a' and 'b' (in that order) supports data-parallelism when merged
bool fully_data_parallel_compatible(const InstrPtr a, const InstrPtr b) {
    if (bh_opcode_is_system(a->opcode) || bh_opcode_is_system(b->opcode))
        return true;
{'fraction_non_alphanumeric': 0.10413671937390193, 'fraction_numerical': 0.011180322632167386, 'mean_word_length': 2.991077119184194, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5141480', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1902, 'n_words': 604}