Question

Suppose there is an array of n elements. Use n threads in CUDA to reduce (sum)...

Suppose there is an array of n elements. Use n threads in CUDA to reduce (sum) these elements to one element. Do “Reduction” using single thread block in CUDA.

Will your program work with multiple thread blocks. What changes are required in case of multiple thread blocks?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java's multithreading support is centered around the java.lang.Thread class. The Thread class provides the capability to create objects of class Thread, each with its own separate flow of control. The Thread class encapsulates the data and methods associated with separate threads of execution and allows multithreading to be integrated within the object-oriented framework.

Java provides two approaches to creating threads. In the first approach, you create a subclass of class Thread and override the run() method to provide an entry point into the thread's execution. When you create an instance of your Thread subclass, you invoke its start() method to cause the thread to execute as an independent sequence of instructions. The start() method is inherited from the Thread class. It initializes the Thread object using your operating system's multithreading capabilities and invokes the run() method. You learn how to create threads using this approach in the next section.

The approach to creating threads identified in the previous paragraph is very simple and straightforward. However, it has the drawback of requiring your Thread objects to be under the Thread class in the class hierarchy. In some cases, as you'll see when you study applets in Part VI, "Programming the Web with Applets and Scripts," this requirement can be somewhat limiting.

Java's other approach to creating threads does not limit the location of your Thread objects within the class hierarchy. In this approach, your class implements the java.lang.Runnable interface. The Runnable interface consists of a single method, the run() method, which must be overridden by your class. The run() method provides an entry point into your thread's execution. In order to run an object of your class as an independent thread, you pass it as an argument to a constructor of class Thread. You learn how to create threads using this approach later in this chapter in the section titled "Implementing Runnable."

Creating Subclasses of Thread

In this section, you create your first multithreaded program by creating a subclass of Thread and then creating, initializing, and starting two Thread objects from your class. The threads will execute concurrently and display Java is hot, aromatic, and invigorating. to the console window.

The source code of the ThreadTest1 program.

class ThreadTest1

{

    public static void main(String args[])

    {

        MyThread thread1 = new MyThread("thread1: ");

        MyThread thread2 = new MyThread("thread2: ");

        thread1.start();

        thread2.start();

        boolean thread1IsAlive = true;

        boolean thread2IsAlive = true;

        do {

           if (thread1IsAlive && !thread1.isAlive()) {

               thread1IsAlive = false;

System.out.println("Thread 1 is dead.");

           }

           if (thread2IsAlive && !thread2.isAlive()) {

               thread2IsAlive = false;

               System.out.println("Thread 2 is dead.");

           }

        } while(thread1IsAlive || thread2IsAlive);

    }

}

class MyThread extends Thread

{

static String message[] =

{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};

    public MyThread(String id)

    {

        super(id);

    }

    public void run()

    {

        String name = getName();

        for (int i=0;i<message.length;++i) {

           randomWait();

           System.out.println(name + message[i]);

        }

    }

    void randomWait()

    {

        try {

           sleep((long)(3000*Math.random()));

        } catch (InterruptedException x) {

           System.out.println("Interrupted!");

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Suppose there is an array of n elements. Use n threads in CUDA to reduce (sum)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. (20%) Suppose you are given the below C code and the corresponding CUDA code: //...

    4. (20%) Suppose you are given the below C code and the corresponding CUDA code: // Invoke DAXPY with 256 threads per Thread Block host nt nbTocks (n 255) /256; /I Invoke DAXPY daxpy(n, 2.0, x, y) // DAXPY in vold daxPy(int n, double a, double .x, double "y) daxpyceenblocks, 256(n, 2.0,x.Y): DAXPY 1n CUDA device void daxpy(int n, double a, double ., double- for (int 1 -0:1n:1 int 1 blockidx.x blockDim.x threadIdx.x (1). (14%) Rewrite the CUDA code so...

  • Given an array A[] of integers find sum of product of all pairs of array elements...

    Given an array A[] of integers find sum of product of all pairs of array elements Input: First line consists of T test cases. First line of every test case consists of N, denoting number of elements of array. Second line consists of elements of array. Output: Single line output, print the sum of products. Constraints: 1<=T<=100 1<=N<=100 Solve the problem using pointers and (if possible) using operators Example: Input: 2 3 1 3 4 4 2 3 4 5...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • //In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 +...

    //In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 + 3*3 + 4*4 + ... + n*n // Note we should know from CSE2500 that this sum is // n*(n+1)*(2*n+1)/6 // We a n value, we will create 2*n threads to do the calculation so that // we can have a race condition. // Before you change the code, read the code and run the code and see what // happens. Do we already...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • 4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n...

    4A. Write a CUDA host program that reads a character type matrix A and integer type matrix B of size mxn. It produces an output string STR such that, every character of A is repeated n times (where n is the integer value in matrix B which is having the same index as that of the character taken in A). Solve this using 1D Block. Example: 1 2 4 3 e X a M Output String STR: pCCaaaaPPPeeXXXXaaaMM 4B. Write...

  • Create a program with threads that looks through a vary large array (100,000,000 elements) to find...

    Create a program with threads that looks through a vary large array (100,000,000 elements) to find the smallest number in that array. You should track the current lowest value seen in a single, shared value. You should also track the time taken, comparing the amount of time for 1, 2, 4 and more threads. Fairly precise timing can be obtained by using the System.nanoTime() method. For example, it's taking my poor computer over 1 second to fill my array with...

  • Write common assembly language (RISC) programs to a) sum the first n elements of an array...

    Write common assembly language (RISC) programs to a) sum the first n elements of an array A, and b) compute r = ab for unsigned integers a and b. Each program will consist of a driver and a subprogram. The drivers will 1) read one or more values from the keyboard, 2) call the subprogram, and 3) print a result. The subprograms must not: ● store into memory, ● use registers $1 – $9, or ● make system calls

  • In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE...

    In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE YOU USE C PROGRAMMING Your MMS will handle all requests of allocation of memory space by different users (one thread per user) …. HINT(You will use Pthreads and Semaphores). Your MMS will provide the user with an interface for making memory requests and also for freeing up memory that is no longer needed by the user. One of the jobs of your memory management...

  • 3. Suppose you have an array of n random elements. You are required to perform n...

    3. Suppose you have an array of n random elements. You are required to perform n different searches on the array. What is best big-oh time for your entire task? Explain how to achieve that time. 4. Suppose you are given two sorted integer arrays int[] A and int[] B. Write a method that returns an array which contains only the common elements (elements that are present in both A and B) of these two sorted arrays. Indicate the big-Oh...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT