{"id":288,"date":"2023-02-23T19:34:11","date_gmt":"2023-02-23T19:34:11","guid":{"rendered":"https:\/\/jtstreib.com\/main\/?page_id=288"},"modified":"2023-02-24T00:59:13","modified_gmt":"2023-02-24T00:59:13","slug":"section-12-4","status":"publish","type":"page","link":"https:\/\/jtstreib.com\/main\/index.php\/section-12-4\/","title":{"rendered":"Section 12.4"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\" style=\"font-size:14px\">\/\/ Guide to Java, Second Edition<br>\/\/ Copyright 2023, by J.T. Streib and T. Soma<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\" style=\"font-size:14px\">public class InnerProductParallel {<br>   public static void main(String[] args) {<br>      int numThreads, vecSize;<br>      int innerProduct;<br>      innerProduct = 0;<br>      numThreads = Integer.parseInt(args[0]);<br>      vecSize = Integer.parseInt(args[1]);<br>      int[] a, b;<br>      a = new int[vecSize];<br>      b = new int[vecSize];<br>      getVec(a);<br>      getVec(b); <br><br>      \/\/ parallel section<br>      \/\/#omp parallel num_threads(numThreads) <br>             shared(numThreads, innerProduct, vecSize, a, b)<br>      {<br>         int myPart, myID, myFirstPos, myLastPos;<br>         int myInnerProduct;<br>         myInnerProduct = 0;<br>         myPart = vecSize \/ numThreads;<br>         myID = Pyjama.omp_get_thread_num();<br>         myFirstPos = myPart * myID;<br>         if(myID == numThreads-1)<br>            myLastPos = vecSize;<br>         else<br>            myLastPos = myFirstPos + myPart;<br><br>         \/\/ multiplication and addition <br>         for(int i=myFirstPos; i&lt;myLastPos; i++)<br>            myInnerProduct = myInnerProduct + (a[i] * b[i]);<br><br>         \/\/ critical section: update innerProduct<br>         \/\/#omp critical <br>         {<br>            innerProduct += myInnerProduct; <br>         } \/\/ end of critical section<br>      } \/\/ end of parallel section<br><br>      \/\/ print the contents of vectors if the size is small<br>      if(vecSize &lt;= 10) {<br>         System.out.print(\"a = [\");<br>         for(int i=0; i&lt;vecSize; i++) <br>            System.out.print(a[i] + \" \");<br>         System.out.println(\"]\");<br>         System.out.print(\"b = [\");<br>         for(int i=0; i&lt;vecSize; i++) <br>            System.out.print(b[i] + \" \");<br>         System.out.println(\"]\");<br>      }<br>      System.out.println(\"Inner Product is \" + innerProduct);<br>   }<br><br>   public static void getVec(int[] array) {<br>      for(int i=0; i&lt;array.length; i++)<br>         array[i] = (int)(Math.floor(Math.random() * 1000));<br>   }<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ Guide to Java, Second Edition\/\/ Copyright 2023, by J.T. Streib and T. Soma public class InnerProductParallel { public static void main(String[] args) { int numThreads, vecSize; int innerProduct; innerProduct = 0; numThreads = Integer.parseInt(args[0]); vecSize = Integer.parseInt(args[1]); int[] a, b; a = new int[vecSize]; b = new int[vecSize]; getVec(a); getVec(b); \/\/ parallel section \/\/#omp&hellip; <a class=\"more-link\" href=\"https:\/\/jtstreib.com\/main\/index.php\/section-12-4\/\">Continue reading <span class=\"screen-reader-text\">Section 12.4<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/pages\/288"}],"collection":[{"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":6,"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/pages\/288\/revisions"}],"predecessor-version":[{"id":316,"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/pages\/288\/revisions\/316"}],"wp:attachment":[{"href":"https:\/\/jtstreib.com\/main\/index.php\/wp-json\/wp\/v2\/media?parent=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}