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 8920237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:13:07+00:00 2026-06-15T06:13:07+00:00

I wrote a kernel for OpenCL where I initialise all the elements of a

  • 0

I wrote a kernel for OpenCL where I initialise all the elements of a 3D array to -> i*i*i + j*j*j. I’m now having problems in creating a grid of threads to do the initialisation of the elements (concurrently). I know that the code that I have now only uses 3 threads, how can I expand on that?

Please help. I’m new to OpenCL, so any suggestion or explanation might be handy. Thanks!

This is code:

_kernel void initialize (
int X;
int Y;
int Z;
_global float*A) {

// Get global position in X direction
int dirX = get_global_id(0);
// Get global position in Y direction
int dirY = get_global_id(1);
// Get global position in Z direction
int dirZ = get_global_id(2);

int A[2000][100][4];
int i,j,k;
for (i=0;i<2000;i++)
{
    for (j=0;j<100;j++)
    {
        for (k=0;k<4;k++)
        {
            A[dirX*X+i][dirY*Y+j][dirZ*Z+k] = i*i*i + j*j*j;
        }
    }
}
}
  • 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-15T06:13:08+00:00Added an answer on June 15, 2026 at 6:13 am
    • You create the buffer to store your output ‘A’ in the calling (host) code. This is passed to your kernel as a pointer, which is correct in your function definition above. However you don’t need to declare it again inside your kernel function, so remove the line int A[2000][100][4];.

    • You can simplify the code greatly. Using the 3D global ID to indicate the 3D index into the array for each work-item, you could change the loop as follows (assuming that for a given i and j, all elements along Z should have the same value):

      __kernel void initialize (__global float* A) {
        // cast required so that kernel compiler knows the array dimensions
        __global float (*a)[2000][100][4] = A;
      
        // Get global position in X direction
        int i = get_global_id(0);
        // Get global position in Y direction
        int j = get_global_id(1);
        // Get global position in Z direction
        int k = get_global_id(2);
      
        (*a)[i][j][k] = i*i*i + j*j*j;
      }
      

    In your calling code you would then create the kernel with a global work-size of 2000x100x4.

    • Practically this is a lot of work items to schedule, so you would likely get better performance from a global (one-dimensional) work-size of 2000 and a loop inside the kernel, e.g.:

      __kernel void initialize (__global float* A) {
        // cast required so that kernel compiler knows the array dimensions
        __global float (*a)[2000][100][4] = A;
      
        // Get global position in X direction
        int i = get_global_id(0);
      
        for (j=0;j<100;j++) {
          for (k=0;k<4;k++) {
            (*a)[i][j][k] = i*i*i + j*j*j;
          }
        }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problems getting any kernel modules to build on my machine. Whenever
Wrote a quick Java proggy to spawn 10 threads with each priority and calculate
Right now I am programming some samples to understand OpenCL for future use. In
I wrote a kernel module and a user that opens it with O_RDWR mode,
I am new to linux kernel programming. I wrote a simple kernel module and
I accidentally wrote a while loop that would never break in a kernel and
I am using Ubuntu-11.04 OS. i wrote a basic interactive kernel module mid.c #include<linux/kernel.h>
I wrote a simply OpenCL program based off the SDK and it compiles and
here's a simple OpenCL Matrix Multiplication kernel which is driving me crazy: By the
I wrote a simple CUDA kernel for Box Filtering of an image. texture<unsigned char,2>

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.