diff --git a/lib/jit/generation/reduce_2d.cpp b/lib/jit/generation/reduce_2d.cpp index 9466feb83..93ec4723c 100644 --- a/lib/jit/generation/reduce_2d.cpp +++ b/lib/jit/generation/reduce_2d.cpp @@ -80,9 +80,8 @@ std::string reduce_2d::generate_impl(std::string const & suffix, expression_tree name[0] += suffix; name[1] += suffix; - int col_simd_width = (reduction_type_ == REDUCE_COLUMNS) ? 1 : p_.simd_width; - unsigned int local_size_0_ld = p_.local_size_0; - std::string ls0ldstr = to_string(local_size_0_ld); + unsigned int ldls = p_.local_size_0; + std::string ls0ldstr = to_string(ldls); auto unroll_tmp = [&]() { @@ -122,44 +121,47 @@ std::string reduce_2d::generate_impl(std::string const & suffix, expression_tree stream.inc_tab(); //Unroll unroll_tmp(); - //Declare Buffers - for (symbolic::reduce_2d* rd : reductions) - stream << rd->process("$LOCAL " + append_width("#scalartype", col_simd_width) + " #name_buf[" + to_string(p_.local_size_1*local_size_0_ld) + "];") << std::endl; - //Loop r - stream << "for($SIZE_T r = $GLOBAL_IDX_1*" << col_simd_width << "; r < (M +" << p_.local_size_1 - 1 << ")/" << p_.local_size_1 << "*" << p_.local_size_1*col_simd_width << "; r += $GLOBAL_SIZE_1*" << col_simd_width << ")" << std::endl; - stream << "{" << std::endl; - stream.inc_tab(); stream << "$SIZE_T lidx = $LOCAL_IDX_0;" << std::endl; stream << "$SIZE_T lidy = $LOCAL_IDX_1;" << std::endl; + //Loop r + std::ostringstream upper; + upper << "(M +" << p_.local_size_1 - 1 << ")/" << p_.local_size_1 << "*" << p_.local_size_1; + + element_wise_loop_1D(stream, p_.fetch_policy, (reduction_type_==REDUCE_ROWS)?p_.simd_width:1, "r", upper.str(), "$GLOBAL_IDX_1", "$GLOBAL_SIZE_1", device, [&](unsigned int cwidth) + { + //Declare Buffers + for (symbolic::reduce_2d* rd : reductions) + stream << rd->process("$LOCAL " + append_width("#scalartype", cwidth) + " #name_buf[" + to_string(p_.local_size_1*ldls) + "];") << std::endl; + //Accumulators for (symbolic::reduce_2d* rd : reductions){ - std::string data_type = append_width("#scalartype",col_simd_width); + std::string data_type = append_width("#scalartype",cwidth); stream << rd->process(data_type + " #name_acc = " + InitPrefix(backend, data_type).get() + "(" + neutral_element((rd)->op(), backend, "#scalartype") + ");") << std::endl; } //Loop c stream << "if (r < M)" << std::endl; stream << "{" << std::endl; stream.inc_tab(); - element_wise_loop_1D(stream, p_.fetch_policy, (reduction_type_==REDUCE_COLUMNS)?p_.simd_width:1, "c", "N", "$GLOBAL_IDX_0", "$GLOBAL_SIZE_0", device, [&](unsigned int row_simd_width) + element_wise_loop_1D(stream, p_.fetch_policy, (reduction_type_==REDUCE_COLUMNS)?p_.simd_width:1, "c", "N", "$GLOBAL_IDX_0", "$GLOBAL_SIZE_0", device, [&](unsigned int rwidth) { - std::string rdtype = append_width("#scalartype", row_simd_width); - std::string cdtype = append_width("#scalartype", col_simd_width); + std::string rdtype = append_width("#scalartype", rwidth); + std::string cdtype = append_width("#scalartype", cwidth); //Fetch std::set fetched; for (symbolic::reduce_2d* rd : reductions) for(symbolic::leaf* sym: symbolic::extract(tree, symbols, rd->root(), false)) if(fetched.insert(sym->process("#name")).second){ if(reduction_type_==REDUCE_COLUMNS) - stream << sym->process(rdtype + " #name = " + append_width("loadv",row_simd_width) + "(c,r);") << std::endl; + stream << sym->process(rdtype + " #name = " + append_width("loadv",rwidth) + "(c,r);") << std::endl; else - stream << sym->process(cdtype + " #name = " + append_width("loadv",col_simd_width) + "(r,c);") << std::endl; + stream << sym->process(cdtype + " #name = " + append_width("loadv",cwidth) + "(r,c);") << std::endl; } //Compute for (symbolic::reduce_2d* rd : reductions) - for (unsigned int s = 0; s < row_simd_width; ++s){ - std::string value = rd->lhs()->evaluate({{"leaf", access_vector_type("#name", s, row_simd_width)}}); + for (unsigned int s = 0; s < rwidth; ++s){ + std::string value = rd->lhs()->evaluate({{"leaf", access_vector_type("#name", s, rwidth)}}); if (is_indexing(rd->op().type)) - compute_index_reduce_1d(stream, rd->process("#name_acc"), "c*"+to_string(row_simd_width) + to_string(s), rd->process("#name_acc_value"), value, rd->op()); + compute_index_reduce_1d(stream, rd->process("#name_acc"), "c*"+to_string(rwidth) + to_string(s), rd->process("#name_acc_value"), value, rd->op()); else compute_reduce_1d(stream, rd->process("#name_acc"), value,rd->op()); } @@ -190,42 +192,24 @@ std::string reduce_2d::generate_impl(std::string const & suffix, expression_tree stream.dec_tab(); stream << "}" << std::endl; //Write result/temporary - stream << "if (lidx == 0 && r < M)" << std::endl; + stream << "if (r < M && lidx == 0)" << std::endl; stream << "{" << std::endl; stream.inc_tab(); if(p_.num_groups_0==1) for(size_t idx: assignments) - for(int s = 0 ; s < col_simd_width ; ++s) + for(size_t s = 0 ; s < cwidth ; ++s) stream << symbols.at(idx)->evaluate({{"leaf", "at(r+" + to_string(s) + ")"}, - {"reduce_2d", access_vector_type("#name_buf[lidy*" + ls0ldstr + "]", s, col_simd_width)}}) << ";" << std::endl; + {"reduce_2d", access_vector_type("#name_buf[lidy*" + ls0ldstr + "]", s, cwidth)}}) << ";" << std::endl; else - { for (symbolic::reduction const * rd : reductions) - { - if(col_simd_width > 1) - stream << "if(M - r > " << col_simd_width << "){" << std::endl; - if (is_indexing(rd->op().type)) - stream << rd->process(vstore(col_simd_width,"uint", "#name_buf_value[lidy*" + ls0ldstr + "]", "0", "#name_temp_value + r + M*$GROUP_IDX_0", "1", backend, false)) << ";" << std::endl; - stream << rd->process(vstore(col_simd_width,"#scalartype", "#name_buf[lidy*" + ls0ldstr + "]", "0", "#name_temp + r + M*$GROUP_IDX_0", "1", backend, false)) << ";" << std::endl; - if(col_simd_width > 1) - { - stream << "}" << std::endl; - stream << "else{" << std::endl; - stream.inc_tab(); - for(int s = 0 ; s < col_simd_width ; ++s){ - if (is_indexing(rd->op().type)) - stream << "if(r + " << s << "< M) " << rd->process("#name_temp_value[r + " + to_string(s) + " + M*$GROUP_IDX_0] = " + access_vector_type("#name_buf_value[lidy*" + ls0ldstr + "]", s)) << ";" << std::endl; - stream << "if(r + " << s << "< M) " << rd->process("#name_temp[r + " + to_string(s) + " + M*$GROUP_IDX_0] = " + access_vector_type("#name_buf[lidy*" + ls0ldstr + "]", s)) << ";" << std::endl; - } - stream.dec_tab(); - stream << "}" << std::endl; + for(size_t s = 0 ; s < cwidth ; ++s){ + if (is_indexing(rd->op().type)) + stream << "if(r + " << s << "< M) " << rd->process("#name_temp_value[r + " + to_string(s) + " + M*$GROUP_IDX_0] = " + access_vector_type("#name_buf_value[lidy*" + ls0ldstr + "]", s, cwidth)) << ";" << std::endl; + stream << "if(r + " << s << "< M) " << rd->process("#name_temp[r + " + to_string(s) + " + M*$GROUP_IDX_0] = " + access_vector_type("#name_buf[lidy*" + ls0ldstr + "]", s, cwidth)) << ";" << std::endl; } - } - } - stream.dec_tab(); - stream << "}" << std::endl; stream.dec_tab(); stream << "}" << std::endl; + }); stream.dec_tab(); stream << "}" << std::endl; @@ -242,7 +226,7 @@ std::string reduce_2d::generate_impl(std::string const & suffix, expression_tree stream.inc_tab(); unroll_tmp(); for (symbolic::reduce_2d* rd : reductions) - stream << rd->process("$LOCAL #scalartype #name_buf[" + to_string(p_.local_size_1*local_size_0_ld) + "];") << std::endl; + stream << rd->process("$LOCAL #scalartype #name_buf[" + to_string(p_.local_size_1*ldls) + "];") << std::endl; stream << "for($SIZE_T r = $GLOBAL_IDX_1; r < (M +" << p_.local_size_1 - 1 << ")/" << p_.local_size_1 << "*" << p_.local_size_1 << "; r += " << GlobalSize1(backend) << "){" << std::endl; stream.inc_tab(); stream << "$SIZE_T lidx = $LOCAL_IDX_0;" << std::endl; diff --git a/tests/api/blas/blas-2.cpp b/tests/api/blas/blas-2.cpp index 167751334..dc468f83d 100644 --- a/tests/api/blas/blas-2.cpp +++ b/tests/api/blas/blas-2.cpp @@ -55,7 +55,7 @@ void test(sc::driver::Context const & ctx, int& nfail, int& npass) int STARTM = std::min(5,M-1), STARTN = std::min(7, N-1), STRIDEM = std::min(3,M); INIT_VECTOR(M, SUBM, STARTM, STRIDEM, cy, y, ctx); INIT_VECTOR(N, SUBN, STARTN, STRIDEN, cx, x, ctx); - INIT_MATRIX(M, SUBM, STARTM, STRIDEM, N, SUBN, STARTN, STRIDEN, cA, A, ctx); + INIT_MATRIX(M, SUBM, STARTM, 1, N, SUBN, STARTN, 1, cA, A, ctx); std::string suffix = "MN"; if(M==1) suffix[0] = '1'; if(N==1) suffix[1] = '1';