Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:05:21+00:00 2026-06-14T01:05:21+00:00

I am teaching myself CUDA from the ground up. I made this simple kernel

  • 0

I am teaching myself CUDA from the ground up. I made this simple kernel that adds 1 to each of the relevant elements within a 2D array. The elements of the 2D array stem from the red channel of an image (zebra.jpg).

from pycuda.compiler import SourceModule
import matplotlib.image as img
import pycuda.driver as cuda
import pycuda.autoinit
import numpy as np

my_image = img.imread("zebra.jpg")[:,:,0]
block = (32, 32, 1)
grid = (8, 8)

if __name__ == '__main__':
  width, height = np.int32(my_image.shape)
  im = np.array(my_image)
  print 'original sum: ' + str(np.sum(im))

  # Create the CUDA kernel, and run it.
  mod = SourceModule("""
    __global__ void add1ToEverything(float* r, int w, int h) {
      int rowID = blockDim.y * blockDim.y + threadIdx.y;
      int colID = blockDim.x * blockIdx.x + threadIdx.x;

      if (rowID > 0 && rowID < h - 2 && colID > 0 && colID < w - 2)
        r[rowID * w + colID] += 1.0;
    }
  """)
  func = mod.get_function('add1ToEverything')

  for i in range(0, 5):
    func(cuda.InOut(im), width, height, block=block, grid=grid)
  print 'new sum: ' + str(np.sum(im))

However, then I run this program, I get the following results.

original sum: 1828815
new sum: 1828815

Why is my original sum identical to my new sum? Shouldn’t the new sum be greater?

Here is zebra.jpg.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:05:24+00:00Added an answer on June 14, 2026 at 1:05 am

    your problem is at the line:
    int rowID = blockDim.y * blockDim.y + threadIdx.y;
    it should be:
    int rowID = blockDim.y * blockIdx.y + threadIdx.y;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been teaching myself programming from scratch by creating a simple app in Objective
I am teaching myself CUDA with the programming guide that CUDA offers. For practice,
I am teaching myself C# (I don't know much yet). In this simple example:
I am teaching myself python from this site . On Chapter 3 , when
I am teaching myself CUDA with pyCUDA. In this exercise, I want to send
Teaching myself the flow of data from mySQL -> PHP -> JSON / Javascript
I'm teaching myself programming and today's challenge is to write a program that can
I've been teaching myself C++ and someone told me that C++ does not have
I am teaching myself jquery it seems while working on this project. I assumed
I am teaching myself machine learning now. One simple question regarding information gain. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.