/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2006 Intel Corporation // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither name of Intel Corporation nor the names of its contributors // may be used to endorse or promote products derived from this software // without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// // This code is for Chapter 2 // Reference to Listing 2.1 // // For compilation, using Intel compiler // User must install Intel compiler, before running this code // // Possible compilation instructions: // // Windows: icl /Qopenmp -o Hello_World_omp Hello_World_omp.c // Linux: icc -openmp -o Hello_World_omp Hello_World_omp.c // // Note: There is a Makefile in this package as well. // The Makefile is used for both OpenMP and pthread examples. #include #include int main() { int threadID, totalThreads; /* OpenMP pragma specifies that following block is going to be parallel and the threadID variable is private in this openmp block. */ #pragma omp parallel private(threadID) { threadID = omp_get_thread_num(); printf("\nHello World is from thread %d\n", (int)threadID); /* Master thread has threadID = 0 */ if (threadID == 0) { printf("\nMaster thread being called\n"); totalThreads = omp_get_num_threads(); printf("Total number of threads are %d\n", totalThreads); } } return 0; }