<commit_before>// -------------------------------------------------------------
// CUDPP -- CUDA Data Parallel Primitives library
// -------------------------------------------------------------
// $Revision: 5636 $
// $Date: 2009-07-02 13:39:38 +1000 (Thu, 02 Jul 2009) $
// ------------------------------------------------------------- 
// This source code is distributed under the terms of license.txt 
// in the root directory of this source distribution.
// ------------------------------------------------------------- 

/**
 * @file
 * reduce_app.cu
 *
 * @brief CUDPP application-level reduction routines
 */
 
#include <stdio.h>

#include "cuda_util.h"
#include "cudpp_plan.h"
#include "cudpp_util.h"
#include "kernel/reduce_kernel.cuh"

/** \addtogroup cudpp_app
  *
  */

/** @name Reduce Functions
 * @{
 */

/**
  * @brief Per-block reduction function
  *
  * This function dispatches the appropriate reduction kernel given the size of the blocks.
  *
  * @param[out] d_odata The output data pointer.  Each block writes a single output element.
  * @param[in]  d_idata The input data pointer.  
  * @param[in]  numElements The number of elements to be reduced.
  * @param[in]  plan A pointer to the plan structure for the reduction.
*/
template <class T, class Oper>
void reduceBlocks(T *d_odata, const T *d_idata, size_t numElements, const CUDPPReducePlan *plan)
{
    unsigned int numThreads = (unsigned int)(((unsigned int)numElements > 2 * plan->m_threadsPerBlock) ?
        plan->m_threadsPerBlock : ceilPow2((unsigned int)numElements) / 2);
    dim3 dimBlock(numThreads, 1, 1);
    unsigned int numBlocks =
        min(plan->m_maxBlocks,
        ((unsigned int)(numElements) +
         (2*plan->m_threadsPerBlock - 1)) / (2*plan->m_threadsPerBlock));

    dim3 dimGrid(numBlocks, 1, 1);
    int smemSize = plan->m_threadsPerBlock * sizeof(T);

    // choose which of the optimized versions of reduction to launch
    
    if (isPowerOfTwo((unsigned int)numElements))
    {
        switch (dimBlock.x)
        {
        case 512:
            reduce<T, Oper, 512, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 256:
            reduce<T, Oper, 256, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 128:
            reduce<T, Oper, 128, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 64:
            reduce<T, Oper, 64, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 32:
            reduce<T, Oper, 32, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 16:<commit_msg>fixed calculation of numThreads when numElements == 1<commit_after>// -------------------------------------------------------------
// CUDPP -- CUDA Data Parallel Primitives library
// -------------------------------------------------------------
// $Revision: 5636 $
// $Date: 2009-07-02 13:39:38 +1000 (Thu, 02 Jul 2009) $
// ------------------------------------------------------------- 
// This source code is distributed under the terms of license.txt 
// in the root directory of this source distribution.
// ------------------------------------------------------------- 

/**
 * @file
 * reduce_app.cu
 *
 * @brief CUDPP application-level reduction routines
 */
 
#include <stdio.h>

#include "cuda_util.h"
#include "cudpp_plan.h"
#include "cudpp_util.h"
#include "kernel/reduce_kernel.cuh"

/** \addtogroup cudpp_app
  *
  */

/** @name Reduce Functions
 * @{
 */

/**
  * @brief Per-block reduction function
  *
  * This function dispatches the appropriate reduction kernel given the size of the blocks.
  *
  * @param[out] d_odata The output data pointer.  Each block writes a single output element.
  * @param[in]  d_idata The input data pointer.  
  * @param[in]  numElements The number of elements to be reduced.
  * @param[in]  plan A pointer to the plan structure for the reduction.
*/
template <class T, class Oper>
void reduceBlocks(T *d_odata, const T *d_idata, size_t numElements, const CUDPPReducePlan *plan)
{
    unsigned int numThreads = (unsigned int)(((unsigned int)numElements > 2 * plan->m_threadsPerBlock) ?
        plan->m_threadsPerBlock : max(1, ceilPow2((unsigned int)numElements) / 2));
    dim3 dimBlock(numThreads, 1, 1);
    unsigned int numBlocks =
        min(plan->m_maxBlocks,
        ((unsigned int)(numElements) +
         (2*plan->m_threadsPerBlock - 1)) / (2*plan->m_threadsPerBlock));

    dim3 dimGrid(numBlocks, 1, 1);
    int smemSize = plan->m_threadsPerBlock * sizeof(T);

    // choose which of the optimized versions of reduction to launch
    
    if (isPowerOfTwo((unsigned int)numElements))
    {
        switch (dimBlock.x)
        {
        case 512:
            reduce<T, Oper, 512, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 256:
            reduce<T, Oper, 256, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 128:
            reduce<T, Oper, 128, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 64:
            reduce<T, Oper, 64, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 32:
            reduce<T, Oper, 32, true><<< dimGrid, dimBlock, smemSize >>>(d_odata, d_idata, (unsigned)numElements); break;
        case 16:
{'fraction_non_alphanumeric': 0.20499461013295006, 'fraction_numerical': 0.024793388429752067, 'mean_word_length': 2.29603315571344, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 61, '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': '5909320', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1666, 'n_words': 494}