Printing in cuda kernel

username: welp
cooked: <p>I had the following code snippet and wanted to check the values of ci and cj. (I have removed most of the code in the kernel for readability)</p>
<pre><code class="lang-auto">#include &lt;stdio.h&gt;
#include &lt;torch/extension.h&gt;

#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;

#include &lt;vector&gt;

namespace {

template &lt;typename scalar_t&gt;
__global__ void sparse_cuda_index_v2_kernel(
    const torch::PackedTensorAccessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt; values,
    const torch::PackedTensorAccessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt; indices,
    const torch::PackedTensorAccessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt; queries,
    const torch::PackedTensorAccessor&lt;scalar_t,1,torch::RestrictPtrTraits,size_t&gt; num_clusters,
    const torch::PackedTensorAccessor&lt;scalar_t,1,torch::RestrictPtrTraits,size_t&gt; cluster_labels,
    const torch::PackedTensorAccessor&lt;scalar_t,1,torch::RestrictPtrTraits,size_t&gt; start_idx,
    torch::PackedTensorAccessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt; sumij,
    torch::PackedTensorAccessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt; sumji,
    torch::PackedTensorAccessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt; outputs)
{
    const long ci = threadIdx.x;
    const long cj = blockIdx.x;
    printf("%d\n",cj);
}
} // namespace

std::vector&lt;torch::Tensor&gt; sparse_cuda_index_v2(
    torch::Tensor values,
    torch::Tensor indices,
    torch::Tensor queries,
    torch::Tensor num_clusters,
    torch::Tensor cluster_labels,
    torch::Tensor outputs) {

    const long Nc = queries.size(0);
    const long threads = Nc; //Value is 5
    const dim3 blocks(Nc,1);
    torch::Tensor sumij = torch::zeros({queries.size(0),values.size(0)}).to(torch::Device(torch::kCUDA, 0));
    torch::Tensor sumji = torch::zeros({queries.size(0),values.size(0)}).to(torch::Device(torch::kCUDA, 0));
    torch::Tensor start_idx = torch::zeros(queries.size(0)).to(torch::Device(torch::kCUDA, 0));
    for(int i=1;i&lt;num_clusters.size(0);i++){
        start_idx[i] = start_idx[i-1] + num_clusters[i-1];
    }
    int64_t total = (start_idx[queries.size(0)-1]+num_clusters[queries.size(0)-1]).item&lt;int64_t&gt;();
    assert(total==cluster_labels.size(0));

    AT_DISPATCH_FLOATING_TYPES(values.type(), "indexing_v2", ([&amp;] {
        sparse_cuda_index_v2_kernel&lt;scalar_t&gt;&lt;&lt;&lt;blocks, threads&gt;&gt;&gt;(
        values.packed_accessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt;(),
        indices.packed_accessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt;(),
        queries.packed_accessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt;(),
        num_clusters.packed_accessor&lt;scalar_t,1,torch::RestrictPtrTraits,size_t&gt;(),
        cluster_labels.packed_accessor&lt;scalar_t,1,torch::RestrictPtrTraits,size_t&gt;(),
        start_idx.packed_accessor&lt;scalar_t,1,torch::RestrictPtrTraits,size_t&gt;(),
        sumij.packed_accessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt;(),
        sumji.packed_accessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt;(),
        outputs.packed_accessor&lt;scalar_t,2,torch::RestrictPtrTraits,size_t&gt;());
    }));
    return {outputs};
}
</code></pre>
<p>This prints all the possible values of cj (0,1,2,3,4) but when I add a print for ci. i.e replace</p>
<pre><code class="lang-auto">    printf("%d\n",cj);

</code></pre>
<p>with</p>
<pre><code class="lang-auto">    printf("%d %d\n",ci, cj);
</code></pre>
<p>cj prints only 0. What’s the reason for this?</p>

username: ptrblck
cooked: <p>I’ve removed some code to just print the thread and block indices and it seems to work fine:</p>
<pre><code class="lang-python">from torch.utils import cpp_extension

cuda_source = """
#include &lt;torch/extension.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;

__global__ void tmp_kernel(void)
{
    const int ci = threadIdx.x;
    const int cj = blockIdx.x;
    printf("Hello from block %d, thread %d ",cj, ci);
}

void tmp_cuda(void) {
    const long threads = 5;
    const dim3 blocks(5);
    tmp_kernel&lt;&lt;&lt;blocks, threads&gt;&gt;&gt;();
    return;
}
"""

cpp_source = """
void tmp_cuda(void);

void tmp(void){
    tmp_cuda();
}
"""

module = torch.utils.cpp_extension.load_inline(
    name="tmp",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions="tmp",
    verbose=True,
)

module.tmp()
</code></pre>