├── .cz-config.js ├── .cz.json ├── ATTRIBUTIONS.md ├── CHANGELOG.md ├── CONTRIBUTING.md ├── CONTRIBUTORS.md ├── LICENSE ├── NOTICE ├── Notes.txt ├── QuickReference.pdf ├── README.md ├── examples └── movie │ ├── build.xml │ ├── movie.bat │ └── src │ └── com │ └── amd │ └── aparapi │ └── examples │ └── movie │ ├── AparapiSolution.java │ ├── ConvMatrix3x3Editor.java │ ├── JJMPEGPlayer.java │ ├── PureJavaSolution.java │ └── ReferenceSolution.java ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── aparapi │ │ └── examples │ │ ├── All.java │ │ ├── add │ │ └── Main.java │ │ ├── afmandelbrot │ │ ├── AfAparapiUtils.java │ │ ├── AfBenchmark.java │ │ ├── AfGUI.java │ │ ├── AfKernel.java │ │ └── AfMain.java │ │ ├── blackscholes │ │ └── Main.java │ │ ├── configuration │ │ ├── AutoCleanUpArraysDemo.java │ │ ├── CleanUpArraysDemo.java │ │ ├── ConfigurationDemo.java │ │ ├── CustomConfigurationDemo.java │ │ ├── KernelOkayInOpenCL.java │ │ ├── KernelWithAlternateFallbackAlgorithm.java │ │ ├── KernelWithoutAlternateFallbackAlgorithm.java │ │ ├── LegacyConfigurationDemo.java │ │ ├── ProfilingDemo.java │ │ └── ProfilingDemoNoBinaryCaching.java │ │ ├── convolution │ │ ├── ConvMatrix3x3Editor.java │ │ ├── Convolution.java │ │ ├── ConvolutionOpenCL.java │ │ ├── ConvolutionViewer.java │ │ └── PureJava.java │ │ ├── effects │ │ └── Main.java │ │ ├── extension │ │ ├── FFTExample.java │ │ ├── Histogram.java │ │ ├── HistogramIdeal.java │ │ ├── JavaMandelBrot.java │ │ ├── JavaMandelBrotMultiThread.java │ │ ├── Mandel.java │ │ ├── MandelBrot.java │ │ ├── MandelExample.java │ │ ├── MandelSimple.java │ │ ├── Pow4Example.java │ │ ├── SquareExample.java │ │ ├── StopWatch.java │ │ └── SwapExample.java │ │ ├── info │ │ └── Main.java │ │ ├── javaonedemo │ │ ├── Life.java │ │ ├── Mandel.java │ │ └── NBody.java │ │ ├── life │ │ └── Main.java │ │ ├── mandel │ │ ├── Main.java │ │ └── Main2D.java │ │ ├── mapreduce │ │ └── Main.java │ │ ├── matrix │ │ ├── CorrMatrixHost.java │ │ ├── CorrMatrixKernel.java │ │ └── Main.java │ │ ├── mdarray │ │ ├── BMatMul1D.java │ │ ├── BMatMul2D.java │ │ ├── BMatMul3D.java │ │ ├── DMatMul1D.java │ │ ├── DMatMul2D.java │ │ ├── DMatMul3D.java │ │ ├── FMatMul1D.java │ │ ├── FMatMul2D.java │ │ ├── FMatMul3D.java │ │ ├── IMatMul1D.java │ │ ├── IMatMul2D.java │ │ ├── IMatMul3D.java │ │ ├── LMatMul1D.java │ │ ├── LMatMul2D.java │ │ ├── LMatMul3D.java │ │ ├── MDArray.java │ │ ├── SMatMul1D.java │ │ ├── SMatMul2D.java │ │ ├── SMatMul3D.java │ │ ├── ZMatMul1D.java │ │ ├── ZMatMul2D.java │ │ └── ZMatMul3D.java │ │ ├── median │ │ ├── MedianDemo.java │ │ ├── MedianKernel7x7.java │ │ └── MedianSettings.java │ │ ├── nbody │ │ ├── Local.java │ │ ├── Main.java │ │ └── Seq.java │ │ ├── oopnbody │ │ ├── Body.java │ │ ├── Main.java │ │ └── Perspective.java │ │ ├── progress │ │ ├── LongRunningKernel.java │ │ ├── MultiPassKernelSwingWorkerDemo.java │ │ └── ProgressAndCancelDemo.java │ │ └── squares │ │ └── Main.java └── resources │ ├── HistogramKernel.cl │ ├── convolution.cl │ ├── duke.jpg │ ├── fft.cl │ ├── knight.png │ ├── log4j.properties │ ├── mandel.cl │ ├── mandel2.cl │ ├── moon.jpg │ ├── particle.jpg │ ├── squarer.cl │ └── testcard.jpg └── test └── java └── com └── aparapi └── examples └── matrix └── CorrMatrixTest.java /.cz-config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 5 | types: [ 6 | {value: 'feat', name: 'feat: A new feature'}, 7 | {value: 'fix', name: 'fix: A bug fix'}, 8 | {value: 'docs', name: 'docs: Documentation only changes'}, 9 | {value: 'style', name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, etc)'}, 10 | {value: 'refactor', name: 'refactor: A code change that neither fixes a bug nor adds a feature'}, 11 | {value: 'perf', name: 'perf: A code change that improves performance'}, 12 | {value: 'test', name: 'test: Adding missing tests or correcting existing tests'}, 13 | {value: 'build', name: 'build: Changes that affect the build system or external dependencies (example scopes: maven, gradle, npm, gulp)'}, 14 | {value: 'ci', name: 'ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)'}, 15 | {value: 'chore', name: 'chore: Other changes that dont modify src or test files'}, 16 | {value: 'revert', name: 'revert: Reverts a previous commit'} 17 | ], 18 | 19 | scopes: [ 20 | {name: 'jni'}, 21 | {name: 'internal'}, 22 | {name: 'swing'}, 23 | {name: 'opencl'} 24 | ], 25 | 26 | scopeOverrides: { 27 | build: [ 28 | {name: 'dependencies'}, 29 | {name: 'versioning'}, 30 | {name: 'release'}, 31 | {name: 'build plugin'} 32 | ], 33 | ci: [ 34 | {name: 'travis'} 35 | ], 36 | chore: [ 37 | {name: 'commitizen'}, 38 | {name: 'editorconfig'}, 39 | {name: 'git'} 40 | ], 41 | docs: [ 42 | {name: 'javadocs'}, 43 | {name: 'repo'}, 44 | {name: 'maven'} 45 | ] 46 | }, 47 | 48 | allowCustomScopes: true, 49 | allowBreakingChanges: ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert'] 50 | 51 | }; 52 | -------------------------------------------------------------------------------- /.cz.json: -------------------------------------------------------------------------------- 1 | { "path": "cz-customizable" } 2 | -------------------------------------------------------------------------------- /ATTRIBUTIONS.md: -------------------------------------------------------------------------------- 1 | This file contains all attributions and other notices that are required by law. 2 | 3 | ## Copyright Notices 4 | 5 | The following are the list of all recognized copyright notices added by contributors to this project: 6 | 7 | Copyright (c) 2016 - 2018, Syncleus, Inc. All rights reserved. 8 | 9 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. All rights reserved. 10 | 11 | ## Licenses 12 | 13 | Aparapi is currently licensed under the Apache License version 2. However it is important to note that the project 14 | owners decided to migrate licenses back in 2016. Under legal advice of a lawyer it was determined the new license, 15 | Apache License version 2, and was fully relicensed as such. This software, therefore, may be destributed under 16 | the Apache License with no additional restrictions beyond those expressed in the Apache License. However, since 17 | both the original license and the Apache License require copyright text and original license text to be provided 18 | for reference the original wording of the license shall be included here as follows: 19 | 20 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 21 | All rights reserved. 22 | 23 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 24 | following conditions are met: 25 | 26 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 27 | disclaimer. 28 | 29 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 30 | disclaimer in the documentation and/or other materials provided with the distribution. 31 | 32 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 33 | derived from this software without specific prior written permission. 34 | 35 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 36 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 37 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 38 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 39 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 40 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 41 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 | 43 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 44 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 45 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 46 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 47 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 48 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 49 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 50 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 51 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 52 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 53 | under those regulations, please refer to the U.S. Bureau of Industry and Security�s website at http://www.bis.doc.gov/. 54 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Aparapi Examples Changelog 2 | 3 | ## 3.0.0 4 | 5 | * Added an interactive Mandlebrot set explorer along with some related benchmarks. 6 | * Updated the following dependency versions: 7 | ** com.aparapi: aparapi 2.0.0 -> 3.0.0 8 | ** org.apache.commons:commons-lang3 3.9 -> 3.12.0 9 | ** org.apache.lucene:lucene-core 8.3.1 -> 8.9.0 10 | ** com.syncleus:syncleus 7 -> 8 11 | 12 | ## 2.0.0 13 | 14 | * Synced to Aparapi 2.0.0 15 | 16 | ## 1.10.0 17 | 18 | * Synced to Aparapi 1.10.0 19 | 20 | ## 1.9.0 21 | 22 | * Synced to Aparapi 1.9.0 23 | 24 | ## v1.8.0 25 | 26 | * Synced to Aparapi 1.8.0 27 | * Updated dependency org.apache.commons:commons-lang3 from 3.6 to 3.7 28 | 29 | 30 | ## 1.7.0 31 | 32 | * Synced to Aparapi 1.7.0 33 | 34 | ## 1.6.0 35 | 36 | * Synced to Aparapi 1.6.0 37 | 38 | ## 1.5.0 39 | 40 | * Synced to Aparapi 1.5.0 41 | 42 | 43 | ## 1.4.1 44 | 45 | * Synced to Aparapi 1.4.1 46 | * Updated parent pon to v6. 47 | * Removed explicit version on nexus-staging-maven-plugin plugin, relies on parent now. 48 | * createProgram had the wrong signature producing a unsatisfied link exception that is now fixed. 49 | * Fixed several bad references to resource locations. 50 | * Fixed all of the broken examples that werent running. 51 | 52 | ## 1.4.0 53 | 54 | * Synced to Aparapi 1.4.0 55 | 56 | ## 1.3.4 57 | 58 | * Synced to Aparapi 1.3.4 59 | * Updated to aparapi-jni 1.1.2 thus fixing `UnsatisfiedLinkError` which occured only on Windows. 60 | 61 | ## 1.3.3 62 | 63 | * Synced to Aparapi 1.3.3 64 | * Fixed "`CXXABI_1.3.8' not found " error encountered on some older systems. 65 | 66 | ## 1.3.2 67 | 68 | * Synced to Aparapi 1.3.2 69 | * Examples now work on Windows 64bit GPUs. 70 | 71 | ## 1.3.1 72 | 73 | * Synced to Aparapi 1.3.1 74 | * Examples now work on Mac OSX SPUs. 75 | 76 | ## 1.3.0 77 | 78 | * Synced to Aparapi 1.3.0. 79 | 80 | ## 1.2.0 81 | 82 | * Synced to Aparapi 1.2.0. 83 | 84 | ## 1.1.2 85 | 86 | * Synced to Aparapi 1.1.2 87 | * Fixed some bugs causing the OpenCL kernel to be compiled twice. 88 | 89 | ## 1.1.1 90 | 91 | * Synced to Aparapi 1.1.1 92 | * Changed package to com.aparapi 93 | 94 | ## 1.1.0 95 | 96 | * Synced to Aparapi 1.1.0 97 | * Changed group id and package to com.aparapi 98 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) 4 | [![Semantic Versioning](https://img.shields.io/SemVer/2.0.0.png)](http://semver.org/spec/v2.0.0.html) 5 | [![Gitter](https://badges.gitter.im/Syncleus/aparapi.svg)](https://gitter.im/Syncleus/aparapi?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 6 | 7 | When contributing to this repository, it is usually a good idea to first discuss the change you 8 | wish to make via issue, email, or any other method with the owners of this repository before 9 | making a change. This could potentially save a lot of wasted hours. 10 | 11 | Please note we have a code of conduct, please follow it in all your interactions with the project. 12 | 13 | ## Development 14 | 15 | ### Commit Message Format 16 | 17 | Starting version 1.3.3 and later all commits on the Syncleus Aparapi repository follow the 18 | [Conventional Changelog standard](https://github.com/conventional-changelog/conventional-changelog-eslint/blob/master/convention.md). 19 | It is a very simple format so you can still write commit messages by hand. However it is 20 | highly recommended developers install [Commitizen](https://commitizen.github.io/cz-cli/), 21 | it extends the git command and will make writing commit messages a breeze. All the Aparapi 22 | repositories are configured with local Commitizen configuration scripts. 23 | 24 | Getting Commitizen installed is usually trivial, just install it via npm. You will also 25 | need to install the cz-customizable adapter which the Aparapi repository is configured 26 | to use. 27 | 28 | ```bash 29 | 30 | npm install -g commitizen@2.8.6 cz-customizable@4.0.0 31 | ``` 32 | 33 | Below is an example of Commitizen in action. It replaces your usual `git commit` command 34 | with `git cz` instead. The new command takes all the same arguments however it leads you 35 | through an interactive process to generate the commit message. 36 | 37 | ![Commitizen friendly](http://aparapi.com/images/commitizen.gif) 38 | 39 | Commit messages are used to automatically generate our changelogs, and to ensure 40 | commits are searchable in a useful way. So please use the Commitizen tool and adhere to 41 | the commit message standard or else we cannot accept Pull Requests without editing 42 | them first. 43 | 44 | Below is an example of a properly formated commit message. 45 | 46 | ``` 47 | chore(Commitizen): Made repository Commitizen friendly. 48 | 49 | Added standard Commitizen configuration files to the repo along with all the custom rules. 50 | 51 | ISSUES CLOSED: #31 52 | ``` 53 | 54 | ### Pull Request Process 55 | 56 | 1. Ensure that install or build dependencies do not appear in any commits in your code branch. 57 | 2. Ensure all commit messages follow the [Conventional Changelog](https://github.com/conventional-changelog/conventional-changelog-eslint/blob/master/convention.md) 58 | standard explained earlier. 59 | 3. Update the CONTRIBUTORS.md file to add your name to it if it isn't already there (one entry 60 | per person). 61 | 4. Adjust the project version to the new version that this Pull Request would represent. The 62 | versioning scheme we use is [Semantic Versioning](http://semver.org/). 63 | 5. Your pull request will either be approved or feedback will be given on what needs to be 64 | fixed to get approval. We usually review and comment on Pull Requests within 48 hours. 65 | 66 | ### Making a Release 67 | 68 | Only administrators with privilages to push to the Aparapi Maven Central account can deploy releases. If this isn't you 69 | then you can just skip this section. 70 | 71 | First ensure the package is prepared for the release process: 72 | 73 | * Make sure any references to the version number in the readme is updated 74 | * Version number in dependency maven snippet. 75 | * Add new version to javadoc version list. 76 | * Ensure that none of the dependencies used are snapshots. 77 | * Update the changelog file. 78 | * Check that all Aparapi libraries used as dependencies point to the latest version. 79 | * After making the release go to the aparapi-docker project and push an image for the new aparapi version 80 | 81 | Next lets take a few steps to do the actual release: 82 | 83 | 1. Update everything listed above. Do **not** drop the package version's `-SNAPSHOT` suffix in master. 84 | 2. Create a release branch, but make sure never to push this branch to the server: `git checkout -b release`. 85 | 3. Update the README.md again to ensure travis badge and javadoc badge point to static tag and not latest. 86 | 4. Drop the `-SNAPSHOT` suffix from the package version. 87 | 5. Commit the current changes using a generic commit message such as `build(release): version 1.2.3`. 88 | 6. Fully test the software before deploying, run all tests and install locally to test against the examples package. 89 | You can install the package locally with `mvn clean install`. 90 | 7. Once satisfied the package is stable deploy it to maven central by executing `mvn -P sign clean package deploy`. 91 | 8. If deployment was successful then create a new tag for the current version with the following command: 92 | `git tag -a v1.2.3 -m "Version 1.2.3"`. 93 | 9. Push the newly created tags to the server: `git push origin v1.2.3:v1.2.3`. 94 | 10. Go to Github and go to the release. Update the description with the changelog for the version and upload 95 | all the artifacts in the target folder. 96 | 10. Checkout master again and then delete the release branch: `git branch -D release`. 97 | 11. Bump the snapshot version of the package to the next expected version, commiting the changes and pushing. 98 | 12. Deploy the new snapshot to the snapshot repository (no need to sign): `mvn clean deploy`. 99 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Contributors 2 | 3 | * Jeffrey Phillips Freeman 4 | * Witold Bolt 5 | * Kenneth Skovhede 6 | * Ryan LaMothe 7 | * Oliver Coleman 8 | * Steven Libby 9 | * Paul Miner 10 | * Lorenzo Gallucci 11 | * Subhomoy Haldar (HungryBlueDev) 12 | * Marco Stefanetti 13 | 14 | # Details 15 | 16 | Below are some of the specific details of various contributions. 17 | 18 | * AMD Corporation for being the founder of the project and contributing most of the code that originally got the project off the ground 19 | * Witold Bolt provided patch for issue #5. Added and tested Mac OS support. Oct 12th 2011 20 | * Kenneth Skovhede provided patch for issue #11 (Correcting conversion of Java Nan and Infinity values to OpenCL equiv). Oct 28th 2011 21 | * Kenneth Skovhede provided patch for issue #12 (Handling of Kernel static fields) Oct 28th 2011. 22 | * Ryan LaMothe provided patch for issue #3 (Support of char - mapped to unsigned short) Nov 28th 2011. 23 | * Ryan LaMothe provided patch for issue #22 (Integrate FindBugs into the Ant build scripts) Feb 18th 2012. 24 | * Ryan LaMothe provided patch for issue #37 (Add Kernel and JNI build support for Applets and JNLP JWS) Feb 18th 2012. 25 | * Oliver Coleman provided patch for issue #64 (Support explicit boolean put and get) Aug 15th 2012 26 | * Steven Libby provided patch for #6 (Allow finer control over fallback mode selection) Aug 21th 2012 27 | * Steven Libby and Ryan Lamothe for #10 (Support for OpenMP, major refactoring cleanup and support for multidim arrays) March 28th 2013 28 | * Paul Miner issue #61 and #115 (JTP Speed up and fixes to explicit puts) June 13th 2013 29 | & lgalluci for his fix for issue #121 (incorrect toString for 3D ranges) July 6th 2013 30 | * HungryBlueDev fixed the JavaDoc errors and warnings. July 8th 2019 -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 - 2018 Syncleus, Inc. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /Notes.txt: -------------------------------------------------------------------------------- 1 | Maybe we can use Java 7's method handles for this? 2 | http://java.sun.com/developer/technicalArticles/DynTypeLang/ 3 | 4 | -------------------------------------------------------------------------------- /QuickReference.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syncleus/aparapi-examples/66a48dc5b4f0f81640cb6be3046426bb02860734/QuickReference.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](http://aparapi.com/images/logo-text-adjacent.png) 2 | 3 | [![License](http://img.shields.io/:license-apache-blue.svg?style=flat-square)](http://www.apache.org/licenses/LICENSE-2.0.html) 4 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.aparapi/aparapi-examples/badge.png?style=flat)](https://maven-badges.herokuapp.com/maven-central/com.aparapi/aparapi-examples/) 5 | [![Gitter](https://badges.gitter.im/Syncleus/aparapi.svg)](https://gitter.im/Syncleus/aparapi?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 6 | 7 | A framework for executing native Java code on the GPU. 8 | 9 | **Licensed under the Apache Software License v2** 10 | 11 | This project is a collection of examples for the Aparapi project. For more information see the Aparapi [website](http://Aparapi.com) or the [QOTO GitLab repository](https://git.qoto.org/aparapi/aparapi-examples). An up-to-date mirror of this repository is also maintained on [Github](https://github.com/Syncleus/aparapi-examples). 12 | 13 | For detailed documentation see [Aparapi.com](http://Aparapi.com). 14 | 15 | For support please use [Gitter](https://gitter.im/Syncleus/aparapi) or the [official Aparapi mailing list and Discourse forum](https://discourse.qoto.org/c/PROJ/APA). 16 | 17 | Please file bugs and feature requests at [QOTO GitLab](https://git.qoto.org/aparapi/aparapi-examples); a mirror of older issues can be found at [Github](https://github.com/Syncleus/aparapi-examples/issues). 18 | 19 | ## Prerequisites 20 | 21 | The examples should run on any system as-is. For GPU acceleration support you must have OpenCL installed and a compatible graphics card. 22 | 23 | **Aparapi runs on all operating systems and platforms, however GPU acceleration support is currently provided for the following platforms: Windows 64bit, Windows 32bit, Mac OSX 64bit, Linux 64bit, and Linux 32bit.** 24 | 25 | ## Obtaining the Source 26 | 27 | The official source repository for Aparapi Examples is located in the Syncleus Github repository and can be cloned using the 28 | following command. 29 | 30 | ```bash 31 | 32 | git clone https://git.qoto.org/aparapi/aparapi-examples.git 33 | ``` 34 | 35 | ## Running 36 | 37 | To run the examples simply checkout the git tag for the version you want to run and execute it through maven. Unless you 38 | specifically want to try the latest snapshot it is important you checkout a specific git tag instead of the master 39 | branch. To use the snapshot in the master branch you will have to manually install the core aparapi snapshot that 40 | matches it since snapshots do not appear in maven central. 41 | 42 | ```bash 43 | 44 | git checkout v3.0.0 45 | mvn clean package exec:java 46 | ``` 47 | -------------------------------------------------------------------------------- /examples/movie/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /examples/movie/movie.bat: -------------------------------------------------------------------------------- 1 | SETLOCAL 2 | if /I %PROCESSOR_ARCHITECTURE%==x86 goto win32 3 | echo "win64!" 4 | set PATH=%PATH%;ffmpeg\ffmpeg-git-9c2651a-win64-shared\bin 5 | set PATH=%PATH%;jjmpeg\jjmpeg-0.0\native\mswin-amd64 6 | goto win64 7 | :win32 8 | echo "win32!" 9 | set PATH=%PATH%;ffmpeg\ffmpeg-git-9c2651a-win32-shared\bin 10 | set PATH=%PATH%;jjmpeg\jjmpeg-0.0\native\mswin-i386 11 | :win64 12 | set PATH=%PATH%;..\..\com.aparapi.jni\dist 13 | java -classpath jjmpeg\jjmpeg-0.0\dist\jjmpeg.jar;..\..\com.aparapi\dist\aparapi.jar;movie.jar; com.aparapi.examples.movie.%1 %2 14 | 15 | ENDLOCAL 16 | -------------------------------------------------------------------------------- /examples/movie/src/com/amd/aparapi/examples/movie/AparapiSolution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | following conditions are met: 7 | 8 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 9 | disclaimer. 10 | 11 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 12 | disclaimer in the documentation and/or other materials provided with the distribution. 13 | 14 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 26 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 27 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 28 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 29 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 30 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 31 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 32 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 33 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 34 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 35 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 36 | 37 | */ 38 | package com.aparapi.examples.movie; 39 | 40 | import java.awt.Graphics2D; 41 | import java.awt.image.BufferedImage; 42 | import java.awt.image.DataBufferByte; 43 | 44 | import com.aparapi.Kernel; 45 | import com.aparapi.Range; 46 | 47 | public class AparapiSolution{ 48 | 49 | public static class AparapiConvolution extends Kernel{ 50 | 51 | private byte[] inputData; 52 | 53 | private byte[] outputData; 54 | 55 | private int width; 56 | 57 | private int height; 58 | 59 | private Range range; 60 | 61 | float[] convMatrix3x3; 62 | 63 | public AparapiConvolution(BufferedImage _imageIn, BufferedImage _imageOut) { 64 | inputData = ((DataBufferByte) _imageIn.getRaster().getDataBuffer()).getData(); 65 | outputData = ((DataBufferByte) _imageOut.getRaster().getDataBuffer()).getData(); 66 | width = _imageIn.getWidth(); 67 | height = _imageIn.getHeight(); 68 | range = Range.create2D(width * 3, height); 69 | setExplicit(true); 70 | 71 | } 72 | 73 | public void processPixel(int x, int y, int w, int h) { 74 | float accum = 0; 75 | int count = 0; 76 | for (int dx = -3; dx < 6; dx += 3) { 77 | for (int dy = -1; dy < 2; dy += 1) { 78 | int rgb = 0xff & inputData[((y + dy) * w) + (x + dx)]; 79 | accum += rgb * convMatrix3x3[count++]; 80 | } 81 | } 82 | outputData[y * w + x] = (byte) Math.max(0, Math.min((int) accum, 255)); 83 | } 84 | 85 | public void run() { 86 | int x = getGlobalId(0); 87 | int y = getGlobalId(1); 88 | int w = getGlobalSize(0); 89 | int h = getGlobalSize(1); 90 | if (x > 3 && x < (w - 3) && y > 1 && y < (h - 1)) { 91 | processPixel(x, y, w, h); 92 | } else { 93 | outputData[y * w + x] = inputData[(y * w) + x]; 94 | } 95 | } 96 | 97 | public void apply(float[] _convMatrix3x3) { 98 | convMatrix3x3 = _convMatrix3x3; 99 | put(convMatrix3x3).put(inputData).execute(range).get(outputData); 100 | } 101 | 102 | } 103 | 104 | public static void main(final String[] _args) { 105 | String fileName = _args.length == 1 ? _args[0] : "Leo720p.wmv"; 106 | 107 | float[] convMatrix3x3 = new float[] { 108 | 0f, 109 | -10f, 110 | 0f, 111 | -10f, 112 | 41f, 113 | -10f, 114 | 0f, 115 | -10f, 116 | 0f 117 | }; 118 | new JJMPEGPlayer("Aparapi - Solution", fileName, convMatrix3x3){ 119 | AparapiConvolution kernel = null; 120 | 121 | @Override protected void processFrame(Graphics2D gc, float[] _convMatrix3x3, BufferedImage in, BufferedImage out) { 122 | if (kernel == null) { 123 | kernel = new AparapiConvolution(in, out); 124 | } 125 | kernel.apply(_convMatrix3x3); 126 | } 127 | }; 128 | 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /examples/movie/src/com/amd/aparapi/examples/movie/ConvMatrix3x3Editor.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | following conditions are met: 7 | 8 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 9 | disclaimer. 10 | 11 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 12 | disclaimer in the documentation and/or other materials provided with the distribution. 13 | 14 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 26 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 27 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 28 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 29 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 30 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 31 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 32 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 33 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 34 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 35 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 36 | 37 | */ 38 | 39 | package com.aparapi.examples.movie; 40 | 41 | import java.awt.BorderLayout; 42 | import java.awt.Component; 43 | import java.awt.GridLayout; 44 | import java.awt.event.ActionEvent; 45 | import java.awt.event.ActionListener; 46 | import java.util.Arrays; 47 | 48 | import javax.swing.BoxLayout; 49 | import javax.swing.JComboBox; 50 | import javax.swing.JPanel; 51 | import javax.swing.JSpinner; 52 | import javax.swing.SpinnerModel; 53 | import javax.swing.SpinnerNumberModel; 54 | import javax.swing.event.ChangeEvent; 55 | import javax.swing.event.ChangeListener; 56 | 57 | public class ConvMatrix3x3Editor{ 58 | Component component; 59 | 60 | float[] default3x3; 61 | 62 | float[] none3x3 = new float[] { 63 | 0, 64 | 0, 65 | 0, 66 | 0, 67 | 1, 68 | 0, 69 | 0, 70 | 0, 71 | 0 72 | }; 73 | 74 | float[] blur3x3 = new float[] { 75 | .1f, 76 | .1f, 77 | .1f, 78 | .1f, 79 | .1f, 80 | .1f, 81 | .1f, 82 | .1f, 83 | .1f 84 | }; 85 | 86 | JSpinner[] spinners = new JSpinner[9]; 87 | 88 | protected void updated(float[] _convMatrix3x3) { 89 | 90 | }; 91 | 92 | void set(float[] _to, float[] _from) { 93 | for (int i = 0; i < 9; i++) { 94 | _to[i] = _from[i]; 95 | spinners[i].setValue((Double) (double) _to[i]); 96 | 97 | } 98 | updated(_to); 99 | } 100 | 101 | ConvMatrix3x3Editor(final float[] _convMatrix3x3) { 102 | default3x3 = Arrays.copyOf(_convMatrix3x3, _convMatrix3x3.length); 103 | JPanel leftPanel = new JPanel(); 104 | JPanel controlPanel = new JPanel(); 105 | BoxLayout layout = new BoxLayout(controlPanel, BoxLayout.Y_AXIS); 106 | controlPanel.setLayout(layout); 107 | component = leftPanel; 108 | JPanel grid3x3Panel = new JPanel(); 109 | controlPanel.add(grid3x3Panel); 110 | grid3x3Panel.setLayout(new GridLayout(3, 3)); 111 | for (int i = 0; i < 9; i++) { 112 | final int index = i; 113 | SpinnerModel model = new SpinnerNumberModel(_convMatrix3x3[index], -50f, 50f, 1f); 114 | JSpinner spinner = new JSpinner(model); 115 | spinners[i] = spinner; 116 | spinner.addChangeListener(new ChangeListener(){ 117 | public void stateChanged(ChangeEvent ce) { 118 | JSpinner source = (JSpinner) ce.getSource(); 119 | double value = ((Double) source.getValue()); 120 | _convMatrix3x3[index] = (float) value; 121 | updated(_convMatrix3x3); 122 | } 123 | }); 124 | grid3x3Panel.add(spinner); 125 | } 126 | String[] options = new String[] { 127 | "DEFAULT", 128 | "NONE", 129 | "BLUR" 130 | }; 131 | JComboBox combo = new JComboBox(options); 132 | combo.addActionListener(new ActionListener(){ 133 | 134 | @Override public void actionPerformed(ActionEvent e) { 135 | JComboBox cb = (JComboBox) e.getSource(); 136 | String value = (String) cb.getSelectedItem(); 137 | if (value.equals("DEFAULT")) { 138 | set(_convMatrix3x3, default3x3); 139 | } else if (value.equals("NONE")) { 140 | set(_convMatrix3x3, none3x3); 141 | } else if (value.equals("BLUR")) { 142 | set(_convMatrix3x3, blur3x3); 143 | } 144 | } 145 | 146 | }); 147 | controlPanel.add(combo); 148 | 149 | leftPanel.add(controlPanel, BorderLayout.NORTH); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /examples/movie/src/com/amd/aparapi/examples/movie/PureJavaSolution.java: -------------------------------------------------------------------------------- 1 | package com.aparapi.examples.movie; 2 | 3 | import java.awt.Graphics2D; 4 | import java.awt.image.ConvolveOp; 5 | 6 | import java.awt.image.BufferedImage; 7 | 8 | public class PureJavaSolution{ 9 | 10 | public static void main(final String[] _args) { 11 | String fileName = _args.length == 1 ? _args[0] : "Leo720p.wmv"; 12 | 13 | float[] convMatrix3x3 = new float[] { 14 | 0f, 15 | -10f, 16 | 0f, 17 | -10f, 18 | 41f, 19 | -10f, 20 | 0f, 21 | -10f, 22 | 0f 23 | }; 24 | 25 | new JJMPEGPlayer("lab_6.alternate", fileName, convMatrix3x3){ 26 | 27 | @Override protected void processFrame(Graphics2D _gc, float[] _convMatrix3x3, BufferedImage _in, BufferedImage _out) { 28 | java.awt.image.Kernel conv = new java.awt.image.Kernel(3, 3, _convMatrix3x3); 29 | ConvolveOp convOp = new ConvolveOp(conv, ConvolveOp.EDGE_NO_OP, null); 30 | convOp.filter(_in, _out); 31 | } 32 | }; 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /examples/movie/src/com/amd/aparapi/examples/movie/ReferenceSolution.java: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | following conditions are met: 7 | 8 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 9 | disclaimer. 10 | 11 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 12 | disclaimer in the documentation and/or other materials provided with the distribution. 13 | 14 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 26 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 27 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 28 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 29 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 30 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 31 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 32 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 33 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 34 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 35 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 36 | 37 | */ 38 | package com.aparapi.examples.movie; 39 | 40 | import java.awt.Graphics2D; 41 | import java.awt.image.BufferedImage; 42 | import java.awt.image.DataBufferByte; 43 | 44 | import com.aparapi.Kernel; 45 | import com.aparapi.Range; 46 | 47 | public class ReferenceSolution{ 48 | 49 | public static class Convolution extends Kernel{ 50 | 51 | private byte[] inputData; 52 | 53 | private byte[] outputData; 54 | 55 | private int width; 56 | 57 | private int height; 58 | 59 | private Range range; 60 | 61 | float[] convMatrix3x3; 62 | 63 | public Convolution(BufferedImage _imageIn, BufferedImage _imageOut) { 64 | inputData = ((DataBufferByte) _imageIn.getRaster().getDataBuffer()).getData(); 65 | outputData = ((DataBufferByte) _imageOut.getRaster().getDataBuffer()).getData(); 66 | width = _imageIn.getWidth(); 67 | height = _imageIn.getHeight(); 68 | range = Range.create2D(width * 3, height); 69 | setExplicit(true); 70 | 71 | } 72 | 73 | public void processPixel(int x, int y, int w, int h) { 74 | float accum = 0; 75 | int count = 0; 76 | for (int dx = -3; dx < 6; dx += 3) { 77 | for (int dy = -1; dy < 2; dy += 1) { 78 | int rgb = 0xff & inputData[((y + dy) * w) + (x + dx)]; 79 | accum += rgb * convMatrix3x3[count++]; 80 | } 81 | } 82 | outputData[y * w + x] = (byte) Math.max(0, Math.min((int) accum, 255)); 83 | } 84 | 85 | public void run() { 86 | int x = getGlobalId(0); 87 | int y = getGlobalId(1); 88 | int w = getGlobalSize(0); 89 | int h = getGlobalSize(1); 90 | if (x > 3 && x < (w - 3) && y > 1 && y < (h - 1)) { 91 | processPixel(x, y, w, h); 92 | } else { 93 | outputData[y * w + x] = inputData[(y * w) + x]; 94 | } 95 | } 96 | 97 | public void apply(float[] _convMatrix3x3) { 98 | convMatrix3x3 = _convMatrix3x3; 99 | for (int x = 0; x < width * 3; x++) { 100 | for (int y = 0; y < height; y++) { 101 | if (x > 3 && x < (width * 3 - 3) && y > 1 && y < (height - 1)) { 102 | processPixel(x, y, width * 3, height); 103 | } 104 | } 105 | } 106 | } 107 | 108 | } 109 | 110 | public static void main(final String[] _args) { 111 | String fileName = _args.length == 1 ? _args[0] : "Leo720p.wmv"; 112 | 113 | float[] convMatrix3x3 = new float[] { 114 | 0f, 115 | -10f, 116 | 0f, 117 | -10f, 118 | 41f, 119 | -10f, 120 | 0f, 121 | -10f, 122 | 0f 123 | }; 124 | new JJMPEGPlayer("Aparapi - Solution", fileName, convMatrix3x3){ 125 | Convolution kernel = null; 126 | 127 | @Override protected void processFrame(Graphics2D gc, float[] _convMatrix3x3, BufferedImage in, BufferedImage out) { 128 | if (kernel == null) { 129 | kernel = new Convolution(in, out); 130 | } 131 | kernel.apply(_convMatrix3x3); 132 | } 133 | }; 134 | 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/add/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | /* 27 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 28 | All rights reserved. 29 | 30 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 31 | following conditions are met: 32 | 33 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 34 | disclaimer. 35 | 36 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 37 | disclaimer in the documentation and/or other materials provided with the distribution. 38 | 39 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 40 | derived from this software without specific prior written permission. 41 | 42 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 43 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 46 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 47 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 48 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 | 50 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 51 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 52 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 53 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 54 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 55 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 56 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 57 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 58 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 59 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 60 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 61 | 62 | */ 63 | 64 | package com.aparapi.examples.add; 65 | 66 | import com.aparapi.Kernel; 67 | import com.aparapi.Range; 68 | 69 | /** 70 | *

Main class.

71 | * 72 | * @author freemo 73 | * @version $Id: $Id 74 | */ 75 | public class Main{ 76 | 77 | /** 78 | *

main.

79 | * 80 | * @param _args an array of {@link java.lang.String} objects. 81 | */ 82 | public static void main(String[] _args) { 83 | 84 | final int size = 512; 85 | 86 | final float[] a = new float[size]; 87 | final float[] b = new float[size]; 88 | 89 | for (int i = 0; i < size; i++) { 90 | a[i] = (float) (Math.random() * 100); 91 | b[i] = (float) (Math.random() * 100); 92 | } 93 | 94 | final float[] sum = new float[size]; 95 | 96 | Kernel kernel = new Kernel(){ 97 | @Override public void run() { 98 | int gid = getGlobalId(); 99 | sum[gid] = a[gid] + b[gid]; 100 | } 101 | }; 102 | 103 | kernel.execute(Range.create(size)); 104 | 105 | for (int i = 0; i < size; i++) { 106 | System.out.printf("%6.2f + %6.2f = %8.2f\n", a[i], b[i], sum[i]); 107 | } 108 | 109 | kernel.dispose(); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/afmandelbrot/AfKernel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.aparapi.examples.afmandelbrot; 17 | 18 | import com.aparapi.Kernel; 19 | 20 | /** 21 | * Aparapi Fractals 22 | * 23 | * the kernel executes the math with complex numbers. Coordinates refer to 24 | * complex plane. result is a vector of number of iterations, It is transformed 25 | * in a color in the GUI, not here 26 | * 27 | * @author marco.stefanetti at gmail.com 28 | * @version $Id: $Id 29 | * @since 2.0.1 30 | */ 31 | public class AfKernel extends Kernel { 32 | 33 | /** the result of all calculations, the iterations for each pixel */ 34 | private int[][] result; 35 | 36 | /** max iterations for pixel */ 37 | private int max_iterations; 38 | 39 | /** starting point, x lower-left */ 40 | private double cx1; 41 | /** starting point, y lower-left */ 42 | private double cy1; 43 | 44 | /** max width */ 45 | private int wmax; 46 | /** max height */ 47 | private int hmax; 48 | 49 | /** one pixel width */ 50 | private double wx; 51 | /** one pixel height */ 52 | private double hy; 53 | 54 | /** 55 | * no values on the constructor, we will reuse the kernel after init 56 | */ 57 | public AfKernel() { 58 | super(); 59 | } 60 | 61 | /** 62 | * sets the parameters, send only few double to the device and a pointer to an 63 | * array to retrieve iterations 64 | * 65 | * @param _cx1 a double. 66 | * @param _cy1 a double. 67 | * @param _cx2 a double. 68 | * @param _cy2 a double. 69 | * @param _W a int. 70 | * @param _H a int. 71 | * @param _max_iterations a int. 72 | */ 73 | public void init(double _cx1, double _cy1, double _cx2, double _cy2, int _W, int _H, int _max_iterations) { 74 | 75 | wmax = _W; 76 | hmax = _H; 77 | result = new int[_W][_H]; 78 | max_iterations = _max_iterations; 79 | cx1 = _cx1; 80 | cy1 = _cy1; 81 | 82 | wx = (_cx2 - cx1) / (double) wmax; 83 | hy = (_cy2 - cy1) / (double) hmax; 84 | 85 | } 86 | 87 | /** 88 | * {@inheritDoc} 89 | * 90 | * just executes the "simple" math on a pixel 91 | */ 92 | @Override 93 | public void run() { 94 | 95 | final int w = getGlobalId(0); 96 | final int h = getGlobalId(1); 97 | 98 | if ((w < wmax) && (h < hmax)) { 99 | 100 | /** from pixel to complex coordinates */ 101 | final double cx = cx1 + w * wx; 102 | final double cy = cy1 + h * hy; 103 | 104 | double xn = cx; 105 | double yn = cy; 106 | 107 | double y2 = cy * cy; 108 | /** I don't save x2, x squared, It's slower **/ 109 | 110 | int t = 0; 111 | 112 | /** 113 | * the original code gave a "goto" error in some platform while( 114 | * (++tGetter for the field result.

131 | * 132 | * @return an array of {@link int} objects. 133 | */ 134 | public int[][] getResult() { 135 | return result; 136 | } 137 | 138 | 139 | 140 | } 141 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/AutoCleanUpArraysDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.examples.mandel.Main; 29 | 30 | /** 31 | *

AutoCleanUpArraysDemo class.

32 | * 33 | * @author freemo 34 | * @version $Id: $Id 35 | */ 36 | public class AutoCleanUpArraysDemo { 37 | /** 38 | *

main.

39 | * 40 | * @param ignored an array of {@link java.lang.String} objects. 41 | */ 42 | public static void main(String[] ignored) { 43 | 44 | System.setProperty("com.aparapi.dumpProfileOnExecution", "true"); 45 | 46 | int size = 1024; 47 | int[] rgbs = new int[size * size]; 48 | Main.MandelKernel kernel = new Main.MandelKernel(size, size, rgbs); 49 | kernel.setAutoCleanUpArrays(true); 50 | kernel.execute(size * size); 51 | System.out.println("length = " + kernel.getRgbs().length); 52 | kernel.resetImage(size, size, rgbs); 53 | kernel.execute(size * size); 54 | System.out.println("length = " + kernel.getRgbs().length); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/CleanUpArraysDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.examples.mandel.Main; 29 | 30 | /** 31 | *

CleanUpArraysDemo class.

32 | * 33 | * @author freemo 34 | * @version $Id: $Id 35 | */ 36 | public class CleanUpArraysDemo { 37 | /** 38 | *

main.

39 | * 40 | * @param ignored an array of {@link java.lang.String} objects. 41 | */ 42 | public static void main(String[] ignored) { 43 | 44 | System.setProperty("com.aparapi.enableVerboseJNI", "true"); 45 | System.setProperty("com.aparapi.enableVerboseJNIOpenCLResourceTracking", "true"); 46 | System.setProperty("com.aparapi.enableExecutionModeReporting", "true"); 47 | System.setProperty("com.aparapi.dumpProfileOnExecution", "true"); 48 | 49 | int size = 1024; 50 | int[] rgbs = new int[size * size]; 51 | Main.MandelKernel kernel = new Main.MandelKernel(size, size, rgbs); 52 | kernel.execute(size * size); 53 | System.out.println("length = " + kernel.getRgbs().length); 54 | System.out.println("Cleaning up arrays"); 55 | kernel.cleanUpArrays(); 56 | System.out.println("length = " + kernel.getRgbs().length); 57 | kernel.resetImage(size, size, rgbs); 58 | kernel.execute(size * size); 59 | System.out.println("length = " + kernel.getRgbs().length); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/ConfigurationDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.internal.kernel.*; 30 | 31 | import java.util.*; 32 | 33 | /** 34 | * Tests device selection via {@link com.aparapi.internal.kernel.KernelManager}. 35 | * 36 | * @author freemo 37 | * @version $Id: $Id 38 | */ 39 | public class ConfigurationDemo { 40 | /** 41 | *

main.

42 | * 43 | * @param ignored an array of {@link java.lang.String} objects. 44 | */ 45 | public static void main(String[] ignored) { 46 | StringBuilder report; 47 | 48 | List tests = Arrays.asList(0, 1, 2, 3); 49 | int reps = 1; 50 | for (int rep = 0; rep < reps; ++rep) { 51 | runTests(rep == 0, tests); 52 | 53 | if (rep % 100 == 99 || rep == 0 || rep == reps - 1) { 54 | report = new StringBuilder("rep = " + rep + "\n"); 55 | KernelManager.instance().reportDeviceUsage(report, true); 56 | System.out.println(report); 57 | } 58 | } 59 | } 60 | 61 | private static void runTests(boolean verbose, List testIndicesToRun) { 62 | final int globalSize = 1; 63 | Kernel kernel; 64 | if (testIndicesToRun.contains(0)) { 65 | if (verbose) { 66 | System.out.println(); 67 | System.out.println("Testing default KernelPreferences with kernel which cannot be run in OpenCL, with fallback algorithm"); 68 | System.out.println(); 69 | } 70 | kernel = new KernelWithAlternateFallbackAlgorithm(); 71 | kernel.execute(globalSize); 72 | kernel.dispose(); 73 | } 74 | 75 | if (testIndicesToRun.contains(1)) { 76 | if (verbose) { 77 | System.out.println(); 78 | System.out.println("Testing default KernelPreferences with kernel which cannot be run in OpenCL, without fallback algorithm"); 79 | System.out.println(); 80 | } 81 | kernel = new KernelWithoutAlternateFallbackAlgorithm(); 82 | kernel.execute(globalSize); 83 | kernel.dispose(); 84 | } 85 | 86 | if (testIndicesToRun.contains(2)) { 87 | if (verbose) { 88 | System.out.println(); 89 | System.out.println("Retesting previous case, should jump straight to regular java implementation without warnings"); 90 | System.out.println(); 91 | } 92 | kernel = new KernelWithoutAlternateFallbackAlgorithm(); 93 | kernel.execute(globalSize); 94 | kernel.dispose(); 95 | } 96 | 97 | if (testIndicesToRun.contains(3)) { 98 | if (verbose) { 99 | System.out.println(); 100 | System.out.println("Testing default KernelPreferences with kernel which should be run in OpenCL"); 101 | System.out.println(); 102 | } 103 | KernelOkayInOpenCL clKernel = new KernelOkayInOpenCL(); 104 | kernel = clKernel; 105 | kernel.execute(clKernel.inChars.length); 106 | String result = new String(clKernel.outChars); 107 | if (verbose) { 108 | System.out.println("kernel output: " + result); 109 | } 110 | kernel.dispose(); 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/CustomConfigurationDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.device.*; 29 | import com.aparapi.internal.kernel.*; 30 | 31 | import java.util.*; 32 | 33 | /** 34 | * Created by Barney on 31/08/2015. 35 | * 36 | * @author freemo 37 | * @version $Id: $Id 38 | */ 39 | public class CustomConfigurationDemo { 40 | 41 | /** 42 | *

main.

43 | * 44 | * @param ignored an array of {@link java.lang.String} objects. 45 | */ 46 | public static void main(String[] ignored) { 47 | System.setProperty("com.aparapi.dumpProfilesOnExit", "true"); 48 | KernelManager manager = new KernelManager() { 49 | @Override 50 | protected List getPreferredDeviceTypes() { 51 | return Arrays.asList(Device.TYPE.CPU, Device.TYPE.ALT, Device.TYPE.JTP); 52 | } 53 | }; 54 | KernelManager.setKernelManager(manager); 55 | 56 | System.out.println("\nTesting custom KernelPreferences with kernel, preferences choose CPU"); 57 | KernelOkayInOpenCL kernel = new KernelOkayInOpenCL(); 58 | kernel.execute(kernel.inChars.length); 59 | System.out.println(kernel.outChars); 60 | 61 | System.out.println("\nTesting custom KernelPreferences with kernel, preferences specify CPU but kernel vetos CPU"); 62 | kernel = new KernelOkayInOpenCL() { 63 | @Override 64 | public boolean isAllowDevice(Device _device) { 65 | return _device.getType() != Device.TYPE.CPU; 66 | } 67 | }; 68 | kernel.execute(kernel.inChars.length); 69 | System.out.println(kernel.outChars); 70 | 71 | StringBuilder report = new StringBuilder("\n"); 72 | KernelManager.instance().reportDeviceUsage(report, true); 73 | System.out.println(report); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/KernelOkayInOpenCL.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | /** 29 | * Created by Barney on 24/08/2015. 30 | * 31 | * @author freemo 32 | * @version $Id: $Id 33 | */ 34 | public class KernelOkayInOpenCL extends com.aparapi.Kernel { 35 | char[] inChars = "KernelOkayInOpenCL".toCharArray(); 36 | char[] outChars = new char[inChars.length]; 37 | 38 | /** {@inheritDoc} */ 39 | @Override 40 | public void run() { 41 | int index = getGlobalId(); 42 | oops(); 43 | outChars[index] = inChars[index]; 44 | } 45 | 46 | @NoCL 47 | private void oops() { 48 | System.out.println("Oops, running in kernel in Java"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/KernelWithAlternateFallbackAlgorithm.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.*; 29 | 30 | /** 31 | * Kernel which will always fail to run on an OpenCLDevice but has an alternative fallback algorithm. 32 | * 33 | * @author freemo 34 | * @version $Id: $Id 35 | */ 36 | public class KernelWithAlternateFallbackAlgorithm extends Kernel { 37 | /** {@inheritDoc} */ 38 | @Override 39 | public void run() { 40 | // deliberately, will fail to generate OpenCL as println is unsupported 41 | System.out.println("Running in Java (regular algorithm)"); 42 | } 43 | 44 | /** {@inheritDoc} */ 45 | @Override 46 | public boolean hasFallbackAlgorithm() { 47 | return true; 48 | } 49 | 50 | /** {@inheritDoc} */ 51 | @Override 52 | public void executeFallbackAlgorithm(Range _range, int _passes) { 53 | System.out.println("Running in Java (alternate non-parallel algorithm)"); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/KernelWithoutAlternateFallbackAlgorithm.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.*; 29 | 30 | /** 31 | * Kernel which will always fail to run on an OpenCLDevice but has an alternative fallback algorithm. 32 | * 33 | * @author freemo 34 | * @version $Id: $Id 35 | */ 36 | public class KernelWithoutAlternateFallbackAlgorithm extends Kernel { 37 | /** {@inheritDoc} */ 38 | @Override 39 | public void run() { 40 | // deliberately, will fail to generate OpenCL as println is unsupported 41 | System.out.println("Running in Java (regular algorithm)"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/LegacyConfigurationDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.internal.kernel.*; 30 | 31 | /** 32 | * Tests device selection when circumventing the {@link com.aparapi.internal.kernel.KernelManager} by using the legacy mechanism 33 | * (setExecutionMode, etc.). 34 | * 35 | * @author freemo 36 | * @version $Id: $Id 37 | */ 38 | public class LegacyConfigurationDemo { 39 | 40 | /** 41 | *

main.

42 | * 43 | * @param ignored an array of {@link java.lang.String} objects. 44 | */ 45 | @SuppressWarnings("deprecation") 46 | public static void main(String[] ignored) { 47 | System.setProperty("com.aparapi.executionMode", "GPU,CPU,SEQ"); 48 | System.setProperty("com.aparapi.dumpProfilesOnExit", "true"); 49 | 50 | KernelWithAlternateFallbackAlgorithm kernel = new KernelWithAlternateFallbackAlgorithm(); 51 | kernel.setExecutionMode(Kernel.EXECUTION_MODE.GPU); 52 | int globalRange = 1; 53 | kernel.execute(globalRange); 54 | 55 | StringBuilder report = new StringBuilder("\n"); 56 | KernelManager.instance().reportDeviceUsage(report, true); 57 | System.out.println(report); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/ProfilingDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.examples.mandel.Main; 29 | import com.aparapi.*; 30 | import com.aparapi.internal.kernel.*; 31 | 32 | /** 33 | * Demonstrate new enhanced profiling capability, profiling the kernel from the blackscholes sample. 34 | * 35 | * @author freemo 36 | * @version $Id: $Id 37 | */ 38 | public class ProfilingDemo { 39 | 40 | private static com.aparapi.examples.blackscholes.Main.BlackScholesKernel kernel; 41 | 42 | /** 43 | *

main.

44 | * 45 | * @param ignored an array of {@link java.lang.String} objects. 46 | */ 47 | public static void main(String[] ignored) { 48 | 49 | final int size = 1024; 50 | newBlackScholesKernel(size); 51 | 52 | // first execute an arbitrary Kernel (not the one we are profiling!) a few times to ensure class loading and initial JIT optimisations have 53 | // been performed before we start the profiling 54 | int warmups = 5; 55 | for (int i = 0; i < warmups; ++i) { 56 | runWarmup(); 57 | } 58 | 59 | String tableHeader = KernelDeviceProfile.getTableHeader(); 60 | 61 | boolean newKernel = false; 62 | 63 | runOnce(size, newKernel); 64 | System.out.println("First run:"); 65 | printLastProfile(tableHeader); 66 | 67 | 68 | int reps = 20; 69 | 70 | System.out.println("\nSubsequent runs using same kernel:"); 71 | for (int rep = 0; rep < reps; ++rep) { 72 | runOnce(size, newKernel); 73 | printLastProfile(tableHeader); 74 | } 75 | 76 | newKernel = true; 77 | System.out.println("\nSubsequent runs using new kernels:"); 78 | for (int rep = 0; rep < reps; ++rep) { 79 | runOnce(size, newKernel); 80 | printLastProfile(tableHeader); 81 | } 82 | 83 | // Note. You will see from the output that there is a substantial cost to Kernel creation (vs Kernel reuse), almost entirely due to KernelRunner#initJNI 84 | 85 | } 86 | 87 | private static void printLastProfile(String tableHeader) { 88 | KernelProfile profile = KernelManager.instance().getProfile(com.aparapi.examples.blackscholes.Main.BlackScholesKernel.class); 89 | KernelDeviceProfile deviceProfile = profile.getLastDeviceProfile(); 90 | String row = deviceProfile.getLastAsTableRow(); 91 | System.out.println(tableHeader); 92 | System.out.println(row); 93 | } 94 | 95 | private static void runOnce(int size, boolean newKernel) { 96 | if (newKernel) { 97 | newBlackScholesKernel(size); 98 | } 99 | kernel.execute(size); 100 | } 101 | 102 | private static void runWarmup() { 103 | int[] rgb = new int[512 * 512]; 104 | Kernel warmupKernel = new Main.MandelKernel(512, 512, rgb); 105 | warmupKernel.execute(512 * 512); 106 | } 107 | 108 | private static void newBlackScholesKernel(int size) { 109 | if (kernel != null) { 110 | kernel.dispose(); 111 | } 112 | System.gc(); 113 | kernel = new com.aparapi.examples.blackscholes.Main.BlackScholesKernel(size); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/configuration/ProfilingDemoNoBinaryCaching.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.configuration; 27 | 28 | import com.aparapi.internal.kernel.*; 29 | 30 | /** 31 | * Created by Barney on 13/09/2015. 32 | * 33 | * @author freemo 34 | * @version $Id: $Id 35 | */ 36 | public class ProfilingDemoNoBinaryCaching { 37 | 38 | /** 39 | *

main.

40 | * 41 | * @param ignored an array of {@link java.lang.String} objects. 42 | */ 43 | public static void main(String[] ignored) { 44 | KernelRunner.BINARY_CACHING_DISABLED = true; 45 | ProfilingDemo.main(null); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/convolution/ConvolutionOpenCL.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | /* 27 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 28 | All rights reserved. 29 | 30 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 31 | following conditions are met: 32 | 33 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 34 | disclaimer. 35 | 36 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 37 | disclaimer in the documentation and/or other materials provided with the distribution. 38 | 39 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 40 | derived from this software without specific prior written permission. 41 | 42 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 43 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 46 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 47 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 48 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 | 50 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 51 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 52 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 53 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 54 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 55 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 56 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 57 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 58 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 59 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 60 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 61 | 62 | */ 63 | 64 | package com.aparapi.examples.convolution; 65 | 66 | import com.aparapi.*; 67 | import com.aparapi.device.*; 68 | import com.aparapi.internal.kernel.*; 69 | import com.aparapi.opencl.*; 70 | import com.aparapi.opencl.OpenCL.*; 71 | 72 | import java.io.*; 73 | import java.net.URISyntaxException; 74 | 75 | /** 76 | *

ConvolutionOpenCL class.

77 | * 78 | * @author freemo 79 | * @version $Id: $Id 80 | */ 81 | public class ConvolutionOpenCL{ 82 | 83 | @Resource("convolution.cl") interface Convolution extends OpenCL{ 84 | Convolution applyConvolution(// 85 | Range range, // 86 | @GlobalReadOnly("_convMatrix3x3") float[] _convMatrix3x3,//// only read from kernel 87 | @GlobalReadOnly("_imagIn") byte[] _imageIn,// only read from kernel (actually char[]) 88 | @GlobalWriteOnly("_imagOut") byte[] _imageOut, // only written to (never read) from kernel (actually char[]) 89 | @Arg("_width") int _width,// 90 | @Arg("_height") int _height); 91 | } 92 | 93 | /** 94 | *

main.

95 | * 96 | * @param _args an array of {@link java.lang.String} objects. 97 | */ 98 | public static void main(final String[] _args) { 99 | final File file; 100 | try{ 101 | file = new File(ConvolutionOpenCL.class.getResource("/testcard.jpg").toURI()); 102 | } catch (URISyntaxException e) { 103 | throw new IllegalStateException("could not get testcard", e); 104 | } 105 | 106 | final OpenCLDevice openclDevice = (OpenCLDevice) KernelManager.instance().bestDevice(); 107 | 108 | final Convolution convolution = openclDevice.bind(Convolution.class); 109 | final float convMatrix3x3[] = new float[] { 110 | 0f, 111 | -10f, 112 | 0f, 113 | -10f, 114 | 40f, 115 | -10f, 116 | 0f, 117 | -10f, 118 | 0f, 119 | }; 120 | 121 | new ConvolutionViewer(file, convMatrix3x3){ 122 | Range range = null; 123 | 124 | @Override protected void applyConvolution(float[] _convMatrix3x3, byte[] _inBytes, byte[] _outBytes, int _width, 125 | int _height) { 126 | if (range == null) { 127 | range = openclDevice.createRange(_width * _height * 3); 128 | } 129 | 130 | convolution.applyConvolution(range, _convMatrix3x3, _inBytes, _outBytes, _width, _height); 131 | } 132 | }; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/convolution/PureJava.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | /* 27 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 28 | All rights reserved. 29 | 30 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 31 | following conditions are met: 32 | 33 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 34 | disclaimer. 35 | 36 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 37 | disclaimer in the documentation and/or other materials provided with the distribution. 38 | 39 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 40 | derived from this software without specific prior written permission. 41 | 42 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 43 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 46 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 47 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 48 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 | 50 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 51 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 52 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 53 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 54 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 55 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 56 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 57 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 58 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 59 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 60 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 61 | 62 | */ 63 | 64 | package com.aparapi.examples.convolution; 65 | 66 | import java.io.File; 67 | import java.net.URISyntaxException; 68 | 69 | import com.aparapi.Kernel; 70 | 71 | /** 72 | *

PureJava class.

73 | * 74 | * @author freemo 75 | * @version $Id: $Id 76 | */ 77 | public class PureJava{ 78 | 79 | final static class ImageConvolution extends Kernel{ 80 | private float convMatrix3x3[]; 81 | 82 | private int width, height; 83 | 84 | private byte imageIn[], imageOut[]; 85 | 86 | public void processPixel(int x, int y, int w, int h) { 87 | float accum = 0f; 88 | int count = 0; 89 | for (int dx = -3; dx < 6; dx += 3) { 90 | for (int dy = -1; dy < 2; dy += 1) { 91 | int rgb = 0xff & imageIn[((y + dy) * w) + (x + dx)]; 92 | 93 | accum += rgb * convMatrix3x3[count++]; 94 | } 95 | } 96 | byte value = (byte) (max(0, min((int) accum, 255))); 97 | imageOut[y * w + x] = value; 98 | 99 | } 100 | 101 | @Override public void run() { 102 | int x = getGlobalId(0) % (width * 3); 103 | int y = getGlobalId(0) / (width * 3); 104 | 105 | if (x > 3 && x < (width * 3 - 3) && y > 1 && y < (height - 1)) { 106 | processPixel(x, y, width * 3, height); 107 | } 108 | 109 | } 110 | 111 | public void applyConvolution(float[] _convMatrix3x3, byte[] _imageIn, byte[] _imageOut, int _width, int _height) { 112 | imageIn = _imageIn; 113 | imageOut = _imageOut; 114 | width = _width; 115 | height = _height; 116 | convMatrix3x3 = _convMatrix3x3; 117 | 118 | execute(3 * width * height); 119 | } 120 | 121 | } 122 | 123 | /** 124 | *

main.

125 | * 126 | * @param _args an array of {@link java.lang.String} objects. 127 | */ 128 | public static void main(final String[] _args) { 129 | final File file; 130 | try{ 131 | file = new File(PureJava.class.getResource("/testcard.jpg").toURI()); 132 | } catch (URISyntaxException e) { 133 | throw new IllegalStateException("could not get testcard", e); 134 | } 135 | 136 | final ImageConvolution convolution = new ImageConvolution(); 137 | 138 | float convMatrix3x3[] = new float[] { 139 | 0f, 140 | -10f, 141 | 0f, 142 | -10f, 143 | 40f, 144 | -10f, 145 | 0f, 146 | -10f, 147 | 0f, 148 | }; 149 | 150 | new ConvolutionViewer(file, convMatrix3x3){ 151 | @Override protected void applyConvolution(float[] _convMatrix3x3, byte[] _inBytes, byte[] _outBytes, int _width, 152 | int _height) { 153 | convolution.applyConvolution(_convMatrix3x3, _inBytes, _outBytes, _width, _height); 154 | } 155 | }; 156 | 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/FFTExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.device.*; 30 | import com.aparapi.internal.kernel.*; 31 | import com.aparapi.opencl.*; 32 | import com.aparapi.opencl.OpenCL.*; 33 | 34 | import java.util.*; 35 | 36 | /** 37 | *

FFTExample class.

38 | * 39 | * @author freemo 40 | * @version $Id: $Id 41 | */ 42 | public class FFTExample{ 43 | 44 | @Resource("fft.cl") 45 | interface FFT extends OpenCL{ 46 | 47 | public FFT forward( 48 | Range _range, 49 | @GlobalReadWrite("real") float[] real, 50 | @GlobalReadWrite("imaginary") float[] imaginary 51 | ); 52 | } 53 | 54 | static void fft(float[] x, float[] y) { 55 | final short dir = 1; 56 | final long m = 10; 57 | int n, i, i1, j, k, i2, l, l1, l2; 58 | double c1, c2, tx, ty, t1, t2, u1, u2, z; 59 | 60 | /* Calculate the number of points */ 61 | n = 1; 62 | for (i = 0; i < m; i++) { 63 | n *= 2; 64 | } 65 | 66 | /* Do the bit reversal */ 67 | i2 = n >> 1; 68 | j = 0; 69 | for (i = 0; i < (n - 1); i++) { 70 | if (i < j) { 71 | tx = x[i]; 72 | ty = y[i]; 73 | x[i] = x[j]; 74 | y[i] = y[j]; 75 | x[j] = (float) tx; 76 | y[j] = (float) ty; 77 | } 78 | k = i2; 79 | while (k <= j) { 80 | j -= k; 81 | k >>= 1; 82 | } 83 | j += k; 84 | } 85 | 86 | /* Compute the FFT */ 87 | c1 = -1.0; 88 | c2 = 0.0; 89 | l2 = 1; 90 | for (l = 0; l < m; l++) { 91 | l1 = l2; 92 | l2 <<= 1; 93 | u1 = 1.0; 94 | u2 = 0.0; 95 | for (j = 0; j < l1; j++) { 96 | for (i = j; i < n; i += l2) { 97 | i1 = i + l1; 98 | t1 = (u1 * x[i1]) - (u2 * y[i1]); 99 | t2 = (u1 * y[i1]) + (u2 * x[i1]); 100 | x[i1] = (float) (x[i] - t1); 101 | y[i1] = (float) (y[i] - t2); 102 | x[i] += (float) t1; 103 | y[i] += (float) t2; 104 | } 105 | z = (u1 * c1) - (u2 * c2); 106 | u2 = (u1 * c2) + (u2 * c1); 107 | u1 = z; 108 | } 109 | c2 = Math.sqrt((1.0 - c1) / 2.0); 110 | if (dir == 1) { 111 | c2 = -c2; 112 | } 113 | c1 = Math.sqrt((1.0 + c1) / 2.0); 114 | } 115 | 116 | /* Scaling for forward transform */ 117 | /*if (dir == 1) { 118 | for (i=0;imain.

128 | * 129 | * @param args an array of {@link java.lang.String} objects. 130 | */ 131 | public static void main(String[] args) { 132 | final int LEN = 1024; 133 | final float initial[] = new float[LEN]; 134 | final float real[] = new float[LEN]; 135 | final float imaginary[] = new float[LEN]; 136 | final float referenceReal[] = Arrays.copyOf(real, real.length); 137 | final float referenceImaginary[] = Arrays.copyOf(imaginary, imaginary.length); 138 | final OpenCLDevice device = (OpenCLDevice) KernelManager.instance().getDefaultPreferences().getPreferredDevice(null); 139 | final FFT fft = device.bind(FFT.class); 140 | for (int i = 0; i < LEN; i++) { 141 | initial[i] = real[i] = referenceReal[i] = (float) (Math.random() * 256); 142 | imaginary[i] = referenceImaginary[0] = 0f; 143 | } 144 | 145 | final Range range = device.createRange(64); 146 | System.out.println("range=" + range); 147 | 148 | final StopWatch timer = new StopWatch(); 149 | timer.start(); 150 | fft.forward(range, real, imaginary); 151 | timer.print("opencl"); 152 | 153 | timer.start(); 154 | fft(referenceReal, referenceImaginary); 155 | timer.print("java"); 156 | for (int i = 0; i < LEN; i++) { 157 | if (Math.abs(real[i] - referenceReal[i]) > 0.01) { 158 | System.out.printf("%d %5.2f %5.2f %5.2f\n", i, initial[i], real[i], referenceReal[i]); 159 | } 160 | } 161 | 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/Histogram.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.Kernel; 29 | import com.aparapi.Range; 30 | import com.aparapi.device.Device; 31 | import com.aparapi.device.OpenCLDevice; 32 | import com.aparapi.opencl.OpenCL; 33 | import com.aparapi.opencl.OpenCL.Resource; 34 | 35 | /** 36 | *

Histogram class.

37 | * 38 | * @author freemo 39 | * @version $Id: $Id 40 | */ 41 | public class Histogram{ 42 | 43 | @Resource("HistogramKernel.cl") interface HistogramKernel extends OpenCL{ 44 | 45 | public HistogramKernel histogram256(// 46 | Range _range,// 47 | @GlobalReadOnly("data") byte[] data,// 48 | @Local("sharedArray") byte[] sharedArray,// 49 | @GlobalWriteOnly("binResult") int[] binResult,// 50 | @Arg("binSize") int binSize); 51 | 52 | public HistogramKernel bin256(// 53 | Range _range,// 54 | @GlobalWriteOnly("histo") int[] histo,// 55 | @GlobalReadOnly("binResult") int[] binResult,// 56 | @Arg("subHistogramSize") int subHistogramSize); 57 | } 58 | 59 | /** 60 | *

main.

61 | * 62 | * @param args an array of {@link java.lang.String} objects. 63 | */ 64 | public static void main(String[] args) { 65 | final int WIDTH = 1024 * 16; 66 | final int HEIGHT = 1024 * 8; 67 | final int BIN_SIZE = 128; 68 | final int GROUP_SIZE = 128; 69 | final int SUB_HISTOGRAM_COUNT = ((WIDTH * HEIGHT) / (GROUP_SIZE * BIN_SIZE)); 70 | 71 | final byte[] data = new byte[WIDTH * HEIGHT]; 72 | for (int i = 0; i < (WIDTH * HEIGHT); i++) { 73 | data[i] = (byte) ((Math.random() * BIN_SIZE) / 2); 74 | } 75 | final byte[] sharedArray = new byte[GROUP_SIZE * BIN_SIZE]; 76 | final int[] binResult = new int[SUB_HISTOGRAM_COUNT * BIN_SIZE]; 77 | System.out.println("binResult size=" + binResult.length); 78 | final int[] histo = new int[BIN_SIZE]; 79 | final int[] refHisto = new int[BIN_SIZE]; 80 | final Kernel k = new Kernel(){ 81 | 82 | @Override public void run() { 83 | final int j = getGlobalId(0); 84 | for (int i = 0; i < SUB_HISTOGRAM_COUNT; ++i) { 85 | histo[j] += binResult[(i * BIN_SIZE) + j]; 86 | } 87 | } 88 | 89 | }; 90 | final Device device = k.getTargetDevice(); 91 | final Range range2 = device.createRange(BIN_SIZE); 92 | k.execute(range2); 93 | 94 | final Range range = Range.create((WIDTH * HEIGHT) / BIN_SIZE, GROUP_SIZE); 95 | 96 | if (device instanceof OpenCLDevice) { 97 | final OpenCLDevice openclDevice = (OpenCLDevice) device; 98 | 99 | final HistogramKernel histogram = openclDevice.bind(HistogramKernel.class); 100 | 101 | final StopWatch timer = new StopWatch(); 102 | timer.start(); 103 | 104 | histogram.histogram256(range, data, sharedArray, binResult, BIN_SIZE); 105 | final boolean java = false; 106 | final boolean aparapiKernel = false; 107 | if (java) { 108 | // Calculate final histogram bin 109 | for (int j = 0; j < BIN_SIZE; ++j) { 110 | for (int i = 0; i < SUB_HISTOGRAM_COUNT; ++i) { 111 | histo[j] += binResult[(i * BIN_SIZE) + j]; 112 | } 113 | } 114 | } else if (aparapiKernel) { 115 | k.execute(range2); 116 | } else { 117 | histogram.bin256(range2, histo, binResult, SUB_HISTOGRAM_COUNT); 118 | } 119 | timer.print("opencl"); 120 | timer.start(); 121 | for (int i = 0; i < (WIDTH * HEIGHT); i++) { 122 | refHisto[data[i]]++; 123 | } 124 | timer.print("java"); 125 | for (int i = 0; i < 128; i++) { 126 | if (refHisto[i] != histo[i]) { 127 | System.out.println(i + " " + histo[i] + " " + refHisto[i]); 128 | } 129 | } 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/HistogramIdeal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.Range; 29 | import com.aparapi.device.Device; 30 | import com.aparapi.device.OpenCLDevice; 31 | import com.aparapi.internal.kernel.*; 32 | import com.aparapi.opencl.OpenCL; 33 | 34 | /** 35 | *

HistogramIdeal class.

36 | * 37 | * @author freemo 38 | * @version $Id: $Id 39 | */ 40 | public class HistogramIdeal{ 41 | 42 | // @Resource("com/amd/aparapi/sample/extension/HistogramKernel.cl") 43 | interface HistogramKernel extends OpenCL{ 44 | 45 | public HistogramKernel histogram256(// 46 | Range _range,// 47 | @GlobalReadOnly("data") byte[] data,// 48 | @Local("sharedArray") byte[] sharedArray,// 49 | @GlobalWriteOnly("binResult") int[] binResult,// 50 | @Arg("binSize") int binSize); 51 | 52 | public HistogramKernel bin256(// 53 | Range _range,// 54 | @GlobalWriteOnly("histo") int[] histo,// 55 | @GlobalReadOnly("binResult") int[] binResult,// 56 | @Arg("subHistogramSize") int subHistogramSize); 57 | } 58 | 59 | /** 60 | *

main.

61 | * 62 | * @param args an array of {@link java.lang.String} objects. 63 | */ 64 | public static void main(String[] args) { 65 | final int WIDTH = 1024 * 16; 66 | final int HEIGHT = 1024 * 8; 67 | final int BIN_SIZE = 128; 68 | final int GROUP_SIZE = 128; 69 | final int SUB_HISTOGRAM_COUNT = ((WIDTH * HEIGHT) / (GROUP_SIZE * BIN_SIZE)); 70 | 71 | final byte[] data = new byte[WIDTH * HEIGHT]; 72 | for (int i = 0; i < (WIDTH * HEIGHT); i++) { 73 | data[i] = (byte) ((Math.random() * BIN_SIZE) / 2); 74 | } 75 | final byte[] sharedArray = new byte[GROUP_SIZE * BIN_SIZE]; 76 | final int[] binResult = new int[SUB_HISTOGRAM_COUNT * BIN_SIZE]; 77 | System.out.println("binResult size=" + binResult.length); 78 | final int[] histo = new int[BIN_SIZE]; 79 | final int[] refHisto = new int[BIN_SIZE]; 80 | final Device device = KernelManager.instance().bestDevice(); 81 | 82 | if (device != null) { 83 | System.out.println(((OpenCLDevice) device).getOpenCLPlatform().getName()); 84 | final Range rangeBinSize = device.createRange(BIN_SIZE); 85 | 86 | final Range range = Range.create((WIDTH * HEIGHT) / BIN_SIZE, GROUP_SIZE); 87 | 88 | if (device instanceof OpenCLDevice) { 89 | final OpenCLDevice openclDevice = (OpenCLDevice) device; 90 | 91 | final HistogramKernel histogram = openclDevice.bind(HistogramKernel.class, Histogram.class.getClassLoader() 92 | .getResourceAsStream("HistogramKernel.cl")); 93 | long start = System.nanoTime(); 94 | histogram.begin()// 95 | .put(data)// 96 | .histogram256(range, data, sharedArray, binResult, BIN_SIZE)// 97 | // by leaving binResult on the GPU we can save two 1Mb transfers 98 | .bin256(rangeBinSize, histo, binResult, SUB_HISTOGRAM_COUNT)// 99 | .get(histo)// 100 | .end(); 101 | System.out.println("opencl " + ((System.nanoTime() - start) / 1000000)); 102 | start = System.nanoTime(); 103 | for (int i = 0; i < (WIDTH * HEIGHT); i++) { 104 | refHisto[data[i]]++; 105 | } 106 | System.out.println("java " + ((System.nanoTime() - start) / 1000000)); 107 | for (int i = 0; i < 128; i++) { 108 | if (refHisto[i] != histo[i]) { 109 | System.out.println(i + " " + histo[i] + " " + refHisto[i]); 110 | } 111 | } 112 | 113 | } 114 | } else { 115 | System.out.println("no GPU device"); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/JavaMandelBrot.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.device.*; 30 | import com.aparapi.internal.kernel.*; 31 | import com.aparapi.opencl.*; 32 | 33 | /** 34 | *

JavaMandelBrot class.

35 | * 36 | * @author freemo 37 | * @version $Id: $Id 38 | */ 39 | public class JavaMandelBrot extends OpenCLAdapter implements MandelBrot{ 40 | final int MAX_ITERATIONS = 64; 41 | 42 | final int pallette[] = new int[] { 43 | -65536, 44 | -59392, 45 | -53248, 46 | -112640, 47 | -106752, 48 | -166144, 49 | -160256, 50 | -219904, 51 | -279552, 52 | -339200, 53 | -399104, 54 | -985344, 55 | -2624000, 56 | -4197376, 57 | -5770496, 58 | -7343872, 59 | -8851712, 60 | -10425088, 61 | -11932928, 62 | -13375232, 63 | -14817792, 64 | -16260096, 65 | -16719602, 66 | -16720349, 67 | -16721097, 68 | -16721846, 69 | -16722595, 70 | -16723345, 71 | -16724351, 72 | -16725102, 73 | -16726110, 74 | -16727119, 75 | -16728129, 76 | -16733509, 77 | -16738889, 78 | -16744269, 79 | -16749138, 80 | -16754006, 81 | -16758619, 82 | -16762976, 83 | -16767077, 84 | -16771178, 85 | -16774767, 86 | -16514932, 87 | -15662970, 88 | -14942079, 89 | -14221189, 90 | -13631371, 91 | -13107088, 92 | -12648342, 93 | -12320669, 94 | -11992995, 95 | -11796393, 96 | -11665328, 97 | -11993019, 98 | -12386248, 99 | -12845011, 100 | -13303773, 101 | -13762534, 102 | -14286830, 103 | -14745588, 104 | -15269881, 105 | -15728637, 106 | -16252927, 107 | 0 108 | }; 109 | 110 | /** {@inheritDoc} */ 111 | @Override public MandelBrot createMandleBrot(Range range, float scale, float offsetx, float offsety, int[] rgb) { 112 | 113 | final int width = range.getGlobalSize(0); 114 | final int height = range.getGlobalSize(1); 115 | for (int gridy = 0; gridy < height; gridy++) { 116 | for (int gridx = 0; gridx < width; gridx++) { 117 | final float x = ((((gridx) * scale) - ((scale / 2.0f) * width)) / width) + offsetx; 118 | final float y = ((((gridy) * scale) - ((scale / 2.0f) * height)) / height) + offsety; 119 | int count = 0; 120 | float zx = x; 121 | float zy = y; 122 | float new_zx = 0.0f; 123 | for (; (count < MAX_ITERATIONS) && (((zx * zx) + (zy * zy)) < 8.0f); count++) { 124 | new_zx = ((zx * zx) - (zy * zy)) + x; 125 | zy = ((2.0f * zx) * zy) + y; 126 | zx = new_zx; 127 | } 128 | rgb[gridx + (gridy * width)] = pallette[count]; 129 | 130 | } 131 | } 132 | return (this); 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/JavaMandelBrotMultiThread.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import java.util.concurrent.BrokenBarrierException; 29 | import java.util.concurrent.CyclicBarrier; 30 | import com.aparapi.*; 31 | import com.aparapi.device.*; 32 | import com.aparapi.internal.kernel.*; 33 | import com.aparapi.opencl.*; 34 | import com.aparapi.Range; 35 | 36 | /** 37 | *

JavaMandelBrotMultiThread class.

38 | * 39 | * @author freemo 40 | * @version $Id: $Id 41 | */ 42 | public class JavaMandelBrotMultiThread extends OpenCLAdapter implements MandelBrot{ 43 | final int MAX_ITERATIONS = 64; 44 | 45 | final int pallette[] = new int[] { 46 | -65536, 47 | -59392, 48 | -53248, 49 | -112640, 50 | -106752, 51 | -166144, 52 | -160256, 53 | -219904, 54 | -279552, 55 | -339200, 56 | -399104, 57 | -985344, 58 | -2624000, 59 | -4197376, 60 | -5770496, 61 | -7343872, 62 | -8851712, 63 | -10425088, 64 | -11932928, 65 | -13375232, 66 | -14817792, 67 | -16260096, 68 | -16719602, 69 | -16720349, 70 | -16721097, 71 | -16721846, 72 | -16722595, 73 | -16723345, 74 | -16724351, 75 | -16725102, 76 | -16726110, 77 | -16727119, 78 | -16728129, 79 | -16733509, 80 | -16738889, 81 | -16744269, 82 | -16749138, 83 | -16754006, 84 | -16758619, 85 | -16762976, 86 | -16767077, 87 | -16771178, 88 | -16774767, 89 | -16514932, 90 | -15662970, 91 | -14942079, 92 | -14221189, 93 | -13631371, 94 | -13107088, 95 | -12648342, 96 | -12320669, 97 | -11992995, 98 | -11796393, 99 | -11665328, 100 | -11993019, 101 | -12386248, 102 | -12845011, 103 | -13303773, 104 | -13762534, 105 | -14286830, 106 | -14745588, 107 | -15269881, 108 | -15728637, 109 | -16252927, 110 | 0 111 | }; 112 | 113 | /** {@inheritDoc} */ 114 | @Override public MandelBrot createMandleBrot(final Range range, final float scale, final float offsetx, final float offsety, 115 | final int[] rgb) { 116 | 117 | final int width = range.getGlobalSize(0); 118 | final int height = range.getGlobalSize(1); 119 | final int threadCount = 8; 120 | final Thread[] threads = new Thread[threadCount]; 121 | final CyclicBarrier barrier = new CyclicBarrier(threadCount + 1); 122 | for (int thread = 0; thread < threadCount; thread++) { 123 | final int threadId = thread; 124 | final int groupHeight = height / threadCount; 125 | (threads[threadId] = new Thread(new Runnable(){ 126 | @Override public void run() { 127 | for (int gridy = threadId * groupHeight; gridy < ((threadId + 1) * groupHeight); gridy++) { 128 | for (int gridx = 0; gridx < width; gridx++) { 129 | final float x = ((((gridx) * scale) - ((scale / 2.0f) * width)) / width) + offsetx; 130 | final float y = ((((gridy) * scale) - ((scale / 2.0f) * height)) / height) + offsety; 131 | int count = 0; 132 | float zx = x; 133 | float zy = y; 134 | float new_zx = 0.0f; 135 | for (; (count < MAX_ITERATIONS) && (((zx * zx) + (zy * zy)) < 8.0f); count++) { 136 | new_zx = ((zx * zx) - (zy * zy)) + x; 137 | zy = ((2.0f * zx) * zy) + y; 138 | zx = new_zx; 139 | } 140 | rgb[gridx + (gridy * width)] = pallette[count]; 141 | } 142 | } 143 | try { 144 | barrier.await(); 145 | } catch (final InterruptedException e) { 146 | // TODO Auto-generated catch block 147 | e.printStackTrace(); 148 | } catch (final BrokenBarrierException e) { 149 | // TODO Auto-generated catch block 150 | e.printStackTrace(); 151 | } 152 | } 153 | })).start(); 154 | } 155 | try { 156 | barrier.await(); 157 | } catch (final InterruptedException e) { 158 | // TODO Auto-generated catch block 159 | e.printStackTrace(); 160 | } catch (final BrokenBarrierException e) { 161 | // TODO Auto-generated catch block 162 | e.printStackTrace(); 163 | } 164 | return (this); 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/Mandel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.device.*; 30 | import com.aparapi.internal.kernel.*; 31 | import com.aparapi.opencl.*; 32 | import com.aparapi.opencl.OpenCL.*; 33 | 34 | import javax.swing.*; 35 | import java.awt.*; 36 | import java.awt.event.*; 37 | import java.awt.image.*; 38 | import java.util.concurrent.*; 39 | import com.aparapi.opencl.OpenCL.Resource; 40 | 41 | /** 42 | * An example Aparapi application which displays a view of the Mandelbrot set and lets the user zoom in to a particular point. 43 | * 44 | * When the user clicks on the view, this example application will zoom in to the clicked point and zoom out there after. 45 | * On GPU, additional computing units will offer a better viewing experience. On the other hand on CPU, this example 46 | * application might suffer with sub-optimal frame refresh rate as compared to GPU. 47 | * 48 | * @author gfrost 49 | * @version $Id: $Id 50 | */ 51 | 52 | @Resource("mandel2.cl") 53 | public interface Mandel extends OpenCL{ 54 | /** 55 | *

createMandleBrot.

56 | * 57 | * @param range a {@link com.aparapi.Range} object. 58 | * @param scale a float. 59 | * @param offsetx a float. 60 | * @param offsety a float. 61 | * @param rgb an array of {@link int} objects. 62 | * @return a {@link com.aparapi.examples.extension.Mandel} object. 63 | */ 64 | Mandel createMandleBrot(// 65 | Range range,// 66 | @Arg("scale") float scale, // 67 | @Arg("offsetx") float offsetx, // 68 | @Arg("offsety") float offsety, // 69 | @GlobalWriteOnly("rgb") int[] rgb); 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/MandelBrot.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.device.*; 30 | import com.aparapi.internal.kernel.*; 31 | import com.aparapi.opencl.*; 32 | import com.aparapi.opencl.OpenCL.Resource; 33 | 34 | /** 35 | * An example Aparapi application which displays a view of the Mandelbrot set and lets the user zoom in to a particular point. 36 | * 37 | * When the user clicks on the view, this example application will zoom in to the clicked point and zoom out there after. 38 | * On GPU, additional computing units will offer a better viewing experience. On the other hand on CPU, this example 39 | * application might suffer with sub-optimal frame refresh rate as compared to GPU. 40 | * 41 | * @author gfrost 42 | * @version $Id: $Id 43 | */ 44 | 45 | @Resource("mandel2.cl") 46 | public interface MandelBrot extends OpenCL{ 47 | /** 48 | *

createMandleBrot.

49 | * 50 | * @param range a {@link com.aparapi.Range} object. 51 | * @param scale a float. 52 | * @param offsetx a float. 53 | * @param offsety a float. 54 | * @param rgb an array of {@link int} objects. 55 | * @return a {@link com.aparapi.examples.extension.MandelBrot} object. 56 | */ 57 | MandelBrot createMandleBrot(// 58 | Range range,// 59 | @Arg("scale") float scale, // 60 | @Arg("offsetx") float offsetx, // 61 | @Arg("offsety") float offsety, // 62 | @GlobalWriteOnly("rgb") int[] rgb); 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/Pow4Example.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.Range; 29 | import com.aparapi.device.Device; 30 | import com.aparapi.device.OpenCLDevice; 31 | import com.aparapi.internal.kernel.*; 32 | import com.aparapi.opencl.OpenCL; 33 | import com.aparapi.opencl.OpenCL.Resource; 34 | 35 | /** 36 | *

Pow4Example class.

37 | * 38 | * @author freemo 39 | * @version $Id: $Id 40 | */ 41 | public class Pow4Example{ 42 | 43 | @Resource("squarer.cl") 44 | interface Squarer extends OpenCL{ 45 | 46 | public Squarer square(// 47 | Range _range,// 48 | @GlobalReadWrite("in") float[] in,// 49 | @GlobalReadWrite("out") float[] out); 50 | } 51 | 52 | /** 53 | *

main.

54 | * 55 | * @param args an array of {@link java.lang.String} objects. 56 | */ 57 | public static void main(String[] args) { 58 | 59 | final int size = 32; 60 | final float[] in = new float[size]; 61 | for (int i = 0; i < size; i++) { 62 | in[i] = i; 63 | } 64 | final float[] squares = new float[size]; 65 | final Range range = Range.create(size); 66 | 67 | final Device device = KernelManager.instance().bestDevice(); 68 | 69 | if (device instanceof OpenCLDevice) { 70 | final OpenCLDevice openclDevice = (OpenCLDevice) device; 71 | 72 | final Squarer squarer = openclDevice.bind(Squarer.class); 73 | squarer.square(range, in, squares); 74 | 75 | for (int i = 0; i < size; i++) { 76 | System.out.println(in[i] + " " + squares[i]); 77 | } 78 | 79 | squarer.square(range, squares, in); 80 | 81 | for (int i = 0; i < size; i++) { 82 | System.out.println(i + " " + squares[i] + " " + in[i]); 83 | } 84 | } 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/SquareExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.ProfileInfo; 29 | import com.aparapi.Range; 30 | import com.aparapi.device.Device; 31 | import com.aparapi.device.OpenCLDevice; 32 | import com.aparapi.internal.kernel.*; 33 | import com.aparapi.opencl.OpenCL; 34 | import com.aparapi.opencl.OpenCL.Resource; 35 | import com.aparapi.opencl.OpenCL.Source; 36 | import java.util.List; 37 | 38 | /** 39 | *

SquareExample class.

40 | * 41 | * @author freemo 42 | * @version $Id: $Id 43 | */ 44 | public class SquareExample{ 45 | 46 | interface Squarer extends OpenCL{ 47 | @Kernel("{\n"// 48 | + " const size_t id = get_global_id(0);\n"// 49 | + " out[id] = in[id]*in[id];\n"// 50 | + "}\n")// 51 | public Squarer square(// 52 | Range _range,// 53 | @GlobalReadWrite("in") float[] in,// 54 | @GlobalReadWrite("out") float[] out); 55 | } 56 | 57 | @Resource("squarer.cl") interface SquarerWithResource extends OpenCL{ 58 | public SquarerWithResource square(// 59 | Range _range,// 60 | @GlobalReadWrite("in") float[] in,// 61 | @GlobalReadWrite("out") float[] out); 62 | } 63 | 64 | @Source("\n"// 65 | + "__kernel void square (\n" // 66 | + " __global float *in,\n"// 67 | + " __global float *out\n" + "){\n"// 68 | + " const size_t id = get_global_id(0);\n"// 69 | + " out[id] = in[id]*in[id];\n"// 70 | + "}\n") interface SquarerWithSource extends OpenCL{ 71 | public SquarerWithSource square(// 72 | Range _range,// 73 | @GlobalReadOnly("in") float[] in,// 74 | @GlobalWriteOnly("out") float[] out); 75 | } 76 | 77 | /** 78 | *

main.

79 | * 80 | * @param args an array of {@link java.lang.String} objects. 81 | */ 82 | public static void main(String[] args) { 83 | final int size = 32; 84 | final float[] in = new float[size]; 85 | 86 | for (int i = 0; i < size; i++) { 87 | in[i] = i; 88 | } 89 | 90 | final float[] squares = new float[size]; 91 | final float[] quads = new float[size]; 92 | final Range range = Range.create(size); 93 | 94 | final Device device = KernelManager.instance().bestDevice(); 95 | 96 | if (device instanceof OpenCLDevice) { 97 | final OpenCLDevice openclDevice = (OpenCLDevice) device; 98 | 99 | for (int l=0; l<5; l++){ 100 | 101 | final SquarerWithResource squarer = openclDevice.bind(SquarerWithResource.class); 102 | squarer.square(range, in, squares); 103 | 104 | for (int i = 0; i < size; i++) { 105 | System.out.println(l+" "+in[i] + " " + squares[i]); 106 | } 107 | 108 | squarer.square(range, squares, quads); 109 | 110 | for (int i = 0; i < size; i++) { 111 | System.out.println(l+" "+ in[i] + " " + squares[i] + " " + quads[i]); 112 | } 113 | final List profileInfo = squarer.getProfileInfo(); 114 | if ((profileInfo != null) && (profileInfo.size() > 0)) { 115 | for (final ProfileInfo p : profileInfo) { 116 | System.out.print(" " + p.getType() + " " + p.getLabel() + " " + (p.getStart() / 1000) + " .. " 117 | + (p.getEnd() / 1000) + " " + ((p.getEnd() - p.getStart()) / 1000) + "us"); 118 | System.out.println(); 119 | } 120 | } 121 | squarer.dispose(); 122 | } 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/StopWatch.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | /** 29 | *

StopWatch class.

30 | * 31 | * @author freemo 32 | * @version $Id: $Id 33 | */ 34 | public class StopWatch{ 35 | long start = 0L; 36 | 37 | /** 38 | *

start.

39 | */ 40 | public void start() { 41 | start = System.nanoTime(); 42 | } 43 | 44 | /** 45 | *

print.

46 | * 47 | * @param _str a {@link java.lang.String} object. 48 | */ 49 | public void print(String _str) { 50 | long end = (System.nanoTime() - start) / 1000000; 51 | System.out.println(_str + " " + end); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/extension/SwapExample.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.extension; 27 | 28 | import com.aparapi.Range; 29 | import com.aparapi.device.Device; 30 | import com.aparapi.device.OpenCLDevice; 31 | import com.aparapi.internal.kernel.*; 32 | import com.aparapi.opencl.OpenCL; 33 | 34 | /** 35 | *

SwapExample class.

36 | * 37 | * @author freemo 38 | * @version $Id: $Id 39 | */ 40 | public class SwapExample{ 41 | 42 | interface Swapper extends OpenCL{ 43 | @Kernel("{\n"// 44 | + " const size_t id = get_global_id(0);\n"// 45 | + " float temp=lhs[id];" + " lhs[id] = rhs[id];\n"// 46 | + " rhs[id] = temp;\n"// 47 | + "}\n")// 48 | public Swapper swap(// 49 | Range _range,// 50 | @GlobalReadWrite("lhs") float[] lhs,// 51 | @GlobalReadWrite("rhs") float[] rhs); 52 | } 53 | 54 | /** 55 | *

main.

56 | * 57 | * @param args an array of {@link java.lang.String} objects. 58 | */ 59 | public static void main(String[] args) { 60 | 61 | final int size = 32; 62 | final float[] lhs = new float[size]; 63 | for (int i = 0; i < size; i++) { 64 | lhs[i] = i; 65 | } 66 | final float[] rhs = new float[size]; 67 | final Range range = Range.create(size); 68 | 69 | final Device device = KernelManager.instance().bestDevice(); 70 | 71 | if (device instanceof OpenCLDevice) { 72 | final OpenCLDevice openclDevice = (OpenCLDevice) device; 73 | 74 | final Swapper swapper = openclDevice.bind(Swapper.class); 75 | for (int i = 0; i < size; i++) { 76 | System.out.println(lhs[i] + " " + rhs[i]); 77 | } 78 | 79 | swapper.swap(range, lhs, rhs); 80 | 81 | for (int i = 0; i < size; i++) { 82 | System.out.println(lhs[i] + " " + rhs[i]); 83 | } 84 | 85 | swapper.swap(range, lhs, rhs); 86 | 87 | for (int i = 0; i < size; i++) { 88 | System.out.println(lhs[i] + " " + rhs[i]); 89 | } 90 | 91 | swapper.swap(range, rhs, lhs); 92 | 93 | for (int i = 0; i < size; i++) { 94 | System.out.println(lhs[i] + " " + rhs[i]); 95 | } 96 | 97 | } 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/info/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | /* 27 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 28 | All rights reserved. 29 | 30 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 31 | following conditions are met: 32 | 33 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 34 | disclaimer. 35 | 36 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 37 | disclaimer in the documentation and/or other materials provided with the distribution. 38 | 39 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 40 | derived from this software without specific prior written permission. 41 | 42 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 43 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 46 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 47 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 48 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 | 50 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 51 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 52 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 53 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 54 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 55 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 56 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 57 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 58 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 59 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 60 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 61 | 62 | */ 63 | 64 | package com.aparapi.examples.info; 65 | 66 | import com.aparapi.device.*; 67 | import com.aparapi.internal.kernel.*; 68 | import com.aparapi.internal.opencl.*; 69 | 70 | import java.util.*; 71 | 72 | /** 73 | *

Main class.

74 | * 75 | * @author freemo 76 | * @version $Id: $Id 77 | */ 78 | public class Main{ 79 | /** 80 | *

main.

81 | * 82 | * @param _args an array of {@link java.lang.String} objects. 83 | */ 84 | public static void main(String[] _args) { 85 | System.out.println("com.aparapi.examples.info.Main"); 86 | List platforms = (new OpenCLPlatform()).getOpenCLPlatforms(); 87 | System.out.println("Machine contains " + platforms.size() + " OpenCL platforms"); 88 | int platformc = 0; 89 | for (OpenCLPlatform platform : platforms) { 90 | System.out.println("Platform " + platformc + "{"); 91 | System.out.println(" Name : \"" + platform.getName() + "\""); 92 | System.out.println(" Vendor : \"" + platform.getVendor() + "\""); 93 | System.out.println(" Version : \"" + platform.getVersion() + "\""); 94 | List devices = platform.getOpenCLDevices(); 95 | System.out.println(" Platform contains " + devices.size() + " OpenCL devices"); 96 | int devicec = 0; 97 | for (OpenCLDevice device : devices) { 98 | System.out.println(" Device " + devicec + "{"); 99 | System.out.println(" Type : " + device.getType()); 100 | System.out.println(" GlobalMemSize : " + device.getGlobalMemSize()); 101 | System.out.println(" LocalMemSize : " + device.getLocalMemSize()); 102 | System.out.println(" MaxComputeUnits : " + device.getMaxComputeUnits()); 103 | System.out.println(" MaxWorkGroupSizes : " + device.getMaxWorkGroupSize()); 104 | System.out.println(" MaxWorkItemDimensions : " + device.getMaxWorkItemDimensions()); 105 | System.out.println(" }"); 106 | devicec++; 107 | } 108 | System.out.println("}"); 109 | platformc++; 110 | } 111 | 112 | KernelPreferences preferences = KernelManager.instance().getDefaultPreferences(); 113 | System.out.println("\nDevices in preferred order:\n"); 114 | 115 | for (Device device : preferences.getPreferredDevices(null)) { 116 | System.out.println(device); 117 | System.out.println(); 118 | } 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mapreduce/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.aparapi.examples.mapreduce; 17 | 18 | import com.aparapi.Kernel; 19 | import com.aparapi.Range; 20 | 21 | import java.util.Arrays; 22 | 23 | /** 24 | *

Main class.

25 | * 26 | * @author freemo 27 | * @version $Id: $Id 28 | */ 29 | public class Main { 30 | /** 31 | *

main.

32 | * 33 | * @param args an array of {@link java.lang.String} objects. 34 | */ 35 | public static void main(String[] args) { 36 | int size = 1048576; 37 | final int count = 3; 38 | final int[] V = new int[size]; 39 | 40 | //lets fill in V randomly... 41 | for (int i = 0; i < size; i++) { 42 | //random number either 0, 1, or 2 43 | V[i] = (int) (Math.random() * 3); 44 | } 45 | 46 | //this will hold our values between the phases. 47 | int[][] totals = new int[count][size]; 48 | 49 | /////////////// 50 | // MAP PHASE // 51 | /////////////// 52 | final int[][] kernelTotals = totals; 53 | Kernel mapKernel = new Kernel() { 54 | @Override 55 | public void run() { 56 | int gid = getGlobalId(); 57 | int value = V[gid]; 58 | for(int index = 0; index < count; index++) { 59 | if (value == index) 60 | kernelTotals[index][gid] = 1; 61 | } 62 | } 63 | }; 64 | mapKernel.execute(Range.create(size)); 65 | mapKernel.dispose(); 66 | totals = kernelTotals; 67 | 68 | ////////////////// 69 | // REDUCE PHASE // 70 | ////////////////// 71 | while (size > 1) { 72 | int nextSize = size / 2; 73 | final int[][] currentTotals = totals; 74 | final int[][] nextTotals = new int[count][nextSize]; 75 | Kernel reduceKernel = new Kernel() { 76 | @Override 77 | public void run() { 78 | int gid = getGlobalId(); 79 | for(int index = 0; index < count; index++) { 80 | nextTotals[index][gid] = currentTotals[index][gid * 2] + currentTotals[index][gid * 2 + 1]; 81 | } 82 | } 83 | }; 84 | reduceKernel.execute(Range.create(nextSize)); 85 | reduceKernel.dispose(); 86 | 87 | totals = nextTotals; 88 | size = nextSize; 89 | } 90 | assert size == 1; 91 | 92 | ///////////////////////////// 93 | // Done, just print it out // 94 | ///////////////////////////// 95 | int[] results = new int[3]; 96 | results[0] = totals[0][0]; 97 | results[1] = totals[1][0]; 98 | results[2] = totals[2][0]; 99 | 100 | System.out.println(Arrays.toString(results)); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/BMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class BMatMul1D extends Kernel{ 31 | byte[] A; 32 | 33 | byte[] B; 34 | 35 | byte[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for BMatMul1D.

41 | * 42 | * @param A an array of {@link byte} objects. 43 | * @param B an array of {@link byte} objects. 44 | * @param C an array of {@link byte} objects. 45 | * @param N a int. 46 | */ 47 | public BMatMul1D(byte[] A, byte[] B, byte[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] += (byte) (A[i * N + k] * B[k * N + j]); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/BMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class BMatMul2D extends Kernel{ 31 | byte[][] A; 32 | 33 | byte[][] B; 34 | 35 | byte[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for BMatMul2D.

41 | * 42 | * @param A an array of {@link byte} objects. 43 | * @param B an array of {@link byte} objects. 44 | * @param C an array of {@link byte} objects. 45 | * @param N a int. 46 | */ 47 | public BMatMul2D(byte[][] A, byte[][] B, byte[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] += (byte) (A[i][k] * B[k][j]); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/BMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class BMatMul3D extends Kernel{ 31 | byte[][][] A; 32 | 33 | byte[][][] B; 34 | 35 | byte[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for BMatMul3D.

41 | * 42 | * @param A an array of {@link byte} objects. 43 | * @param B an array of {@link byte} objects. 44 | * @param C an array of {@link byte} objects. 45 | * @param N a int. 46 | */ 47 | public BMatMul3D(byte[][][] A, byte[][][] B, byte[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | int a0 = A.length; 61 | int a1 = A[0].length; 62 | int a2 = A[0][0].length; 63 | for (int l = 0; l < N; l++) { 64 | C[i][j][k] += (byte) (A[i][j][l] * B[l][j][k]); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/DMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class DMatMul1D extends Kernel{ 31 | double[] A; 32 | 33 | double[] B; 34 | 35 | double[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for DMatMul1D.

41 | * 42 | * @param A an array of {@link double} objects. 43 | * @param B an array of {@link double} objects. 44 | * @param C an array of {@link double} objects. 45 | * @param N a int. 46 | */ 47 | public DMatMul1D(double[] A, double[] B, double[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] += A[i * N + k] * B[k * N + j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/DMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class DMatMul2D extends Kernel{ 31 | double[][] A; 32 | 33 | double[][] B; 34 | 35 | double[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for DMatMul2D.

41 | * 42 | * @param A an array of {@link double} objects. 43 | * @param B an array of {@link double} objects. 44 | * @param C an array of {@link double} objects. 45 | * @param N a int. 46 | */ 47 | public DMatMul2D(double[][] A, double[][] B, double[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] += A[i][k] * B[k][j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/DMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class DMatMul3D extends Kernel{ 31 | double[][][] A; 32 | 33 | double[][][] B; 34 | 35 | double[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for DMatMul3D.

41 | * 42 | * @param A an array of {@link double} objects. 43 | * @param B an array of {@link double} objects. 44 | * @param C an array of {@link double} objects. 45 | * @param N a int. 46 | */ 47 | public DMatMul3D(double[][][] A, double[][][] B, double[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | for (int l = 0; l < N; l++) { 61 | C[i][j][k] += A[i][j][l] * B[l][j][k]; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/FMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class FMatMul1D extends Kernel{ 31 | float[] A; 32 | 33 | float[] B; 34 | 35 | float[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for FMatMul1D.

41 | * 42 | * @param A an array of {@link float} objects. 43 | * @param B an array of {@link float} objects. 44 | * @param C an array of {@link float} objects. 45 | * @param N a int. 46 | */ 47 | public FMatMul1D(float[] A, float[] B, float[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] += A[i * N + k] * B[k * N + j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/FMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class FMatMul2D extends Kernel{ 31 | float[][] A; 32 | 33 | float[][] B; 34 | 35 | float[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for FMatMul2D.

41 | * 42 | * @param A an array of {@link float} objects. 43 | * @param B an array of {@link float} objects. 44 | * @param C an array of {@link float} objects. 45 | * @param N a int. 46 | */ 47 | public FMatMul2D(float[][] A, float[][] B, float[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] += A[i][k] * B[k][j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/FMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class FMatMul3D extends Kernel{ 31 | float[][][] A; 32 | 33 | float[][][] B; 34 | 35 | float[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for FMatMul3D.

41 | * 42 | * @param A an array of {@link float} objects. 43 | * @param B an array of {@link float} objects. 44 | * @param C an array of {@link float} objects. 45 | * @param N a int. 46 | */ 47 | public FMatMul3D(float[][][] A, float[][][] B, float[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | for (int l = 0; l < N; l++) { 61 | C[i][j][k] += A[i][j][l] * B[l][j][k]; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/IMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class IMatMul1D extends Kernel{ 31 | int[] A; 32 | 33 | int[] B; 34 | 35 | int[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for IMatMul1D.

41 | * 42 | * @param A an array of {@link int} objects. 43 | * @param B an array of {@link int} objects. 44 | * @param C an array of {@link int} objects. 45 | * @param N a int. 46 | */ 47 | public IMatMul1D(int[] A, int[] B, int[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] += A[i * N + k] * B[k * N + j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/IMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class IMatMul2D extends Kernel{ 31 | int[][] A; 32 | 33 | int[][] B; 34 | 35 | int[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for IMatMul2D.

41 | * 42 | * @param A an array of {@link int} objects. 43 | * @param B an array of {@link int} objects. 44 | * @param C an array of {@link int} objects. 45 | * @param N a int. 46 | */ 47 | public IMatMul2D(int[][] A, int[][] B, int[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] += A[i][k] * B[k][j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/IMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class IMatMul3D extends Kernel{ 31 | int[][][] A; 32 | 33 | int[][][] B; 34 | 35 | int[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for IMatMul3D.

41 | * 42 | * @param A an array of {@link int} objects. 43 | * @param B an array of {@link int} objects. 44 | * @param C an array of {@link int} objects. 45 | * @param N a int. 46 | */ 47 | public IMatMul3D(int[][][] A, int[][][] B, int[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | for (int l = 0; l < N; l++) { 61 | C[i][j][k] += A[i][j][l] * B[l][j][k]; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/LMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class LMatMul1D extends Kernel{ 31 | long[] A; 32 | 33 | long[] B; 34 | 35 | long[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for LMatMul1D.

41 | * 42 | * @param A an array of {@link long} objects. 43 | * @param B an array of {@link long} objects. 44 | * @param C an array of {@link long} objects. 45 | * @param N a int. 46 | */ 47 | public LMatMul1D(long[] A, long[] B, long[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] += A[i * N + k] * B[k * N + j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/LMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class LMatMul2D extends Kernel{ 31 | long[][] A; 32 | 33 | long[][] B; 34 | 35 | long[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for LMatMul2D.

41 | * 42 | * @param A an array of {@link long} objects. 43 | * @param B an array of {@link long} objects. 44 | * @param C an array of {@link long} objects. 45 | * @param N a int. 46 | */ 47 | public LMatMul2D(long[][] A, long[][] B, long[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] += A[i][k] * B[k][j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/LMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class LMatMul3D extends Kernel{ 31 | long[][][] A; 32 | 33 | long[][][] B; 34 | 35 | long[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for LMatMul3D.

41 | * 42 | * @param A an array of {@link long} objects. 43 | * @param B an array of {@link long} objects. 44 | * @param C an array of {@link long} objects. 45 | * @param N a int. 46 | */ 47 | public LMatMul3D(long[][][] A, long[][][] B, long[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | for (int l = 0; l < N; l++) { 61 | C[i][j][k] += A[i][j][l] * B[l][j][k]; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/SMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class SMatMul1D extends Kernel{ 31 | short[] A; 32 | 33 | short[] B; 34 | 35 | short[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for SMatMul1D.

41 | * 42 | * @param A an array of {@link short} objects. 43 | * @param B an array of {@link short} objects. 44 | * @param C an array of {@link short} objects. 45 | * @param N a int. 46 | */ 47 | public SMatMul1D(short[] A, short[] B, short[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] += (short) (A[i * N + k] * B[k * N + j]); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/SMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class SMatMul2D extends Kernel{ 31 | short[][] A; 32 | 33 | short[][] B; 34 | 35 | short[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for SMatMul2D.

41 | * 42 | * @param A an array of {@link short} objects. 43 | * @param B an array of {@link short} objects. 44 | * @param C an array of {@link short} objects. 45 | * @param N a int. 46 | */ 47 | public SMatMul2D(short[][] A, short[][] B, short[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] += (short) (A[i][k] * B[k][j]); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/SMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class SMatMul3D extends Kernel{ 31 | short[][][] A; 32 | 33 | short[][][] B; 34 | 35 | short[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for SMatMul3D.

41 | * 42 | * @param A an array of {@link short} objects. 43 | * @param B an array of {@link short} objects. 44 | * @param C an array of {@link short} objects. 45 | * @param N a int. 46 | */ 47 | public SMatMul3D(short[][][] A, short[][][] B, short[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | for (int l = 0; l < N; l++) { 61 | C[i][j][k] += (short) (A[i][j][l] * B[l][j][k]); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/ZMatMul1D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class ZMatMul1D extends Kernel{ 31 | boolean[] A; 32 | 33 | boolean[] B; 34 | 35 | boolean[] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for ZMatMul1D.

41 | * 42 | * @param A an array of {@link boolean} objects. 43 | * @param B an array of {@link boolean} objects. 44 | * @param C an array of {@link boolean} objects. 45 | * @param N a int. 46 | */ 47 | public ZMatMul1D(boolean[] A, boolean[] B, boolean[] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i * N + j] ^= A[i * N + k] & B[k * N + j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/ZMatMul2D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class ZMatMul2D extends Kernel{ 31 | boolean[][] A; 32 | 33 | boolean[][] B; 34 | 35 | boolean[][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for ZMatMul2D.

41 | * 42 | * @param A an array of {@link boolean} objects. 43 | * @param B an array of {@link boolean} objects. 44 | * @param C an array of {@link boolean} objects. 45 | * @param N a int. 46 | */ 47 | public ZMatMul2D(boolean[][] A, boolean[][] B, boolean[][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / N; 58 | int j = id % N; 59 | for (int k = 0; k < N; k++) { 60 | C[i][j] ^= A[i][k] & B[k][j]; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/mdarray/ZMatMul3D.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.mdarray; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | class ZMatMul3D extends Kernel{ 31 | boolean[][][] A; 32 | 33 | boolean[][][] B; 34 | 35 | boolean[][][] C; 36 | 37 | int N; 38 | 39 | /** 40 | *

Constructor for ZMatMul3D.

41 | * 42 | * @param A an array of {@link boolean} objects. 43 | * @param B an array of {@link boolean} objects. 44 | * @param C an array of {@link boolean} objects. 45 | * @param N a int. 46 | */ 47 | public ZMatMul3D(boolean[][][] A, boolean[][][] B, boolean[][][] C, int N) { 48 | this.A = A; 49 | this.B = B; 50 | this.C = C; 51 | this.N = N; 52 | } 53 | 54 | /** {@inheritDoc} */ 55 | @Override public void run() { 56 | int id = getGlobalId(); 57 | int i = id / (N * N); 58 | int j = (id / N) % N; 59 | int k = id % N; 60 | for (int l = 0; l < N; l++) { 61 | C[i][j][k] ^= A[i][j][l] & B[l][j][k]; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/median/MedianDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.median; 27 | 28 | import com.aparapi.internal.kernel.*; 29 | 30 | import javax.imageio.*; 31 | import javax.swing.*; 32 | import java.awt.*; 33 | import java.awt.image.*; 34 | import java.io.*; 35 | import java.net.URISyntaxException; 36 | 37 | /** 38 | * Demonstrate use of __private namespaces and @NoCL annotations. 39 | * 40 | * @author freemo 41 | * @version $Id: $Id 42 | */ 43 | public class MedianDemo { 44 | /** Constant testImage */ 45 | public static BufferedImage testImage; 46 | 47 | static { 48 | try { 49 | File imageFile = new File(MedianDemo.class.getResource("/testcard.jpg").toURI()).getCanonicalFile(); 50 | if (imageFile.exists()) { 51 | testImage = ImageIO.read(imageFile); 52 | } 53 | } catch (IOException | URISyntaxException e) { 54 | throw new IllegalStateException("Could not open image", e); 55 | } 56 | } 57 | 58 | /** 59 | *

main.

60 | * 61 | * @param ignored an array of {@link java.lang.String} objects. 62 | */ 63 | public static void main(String[] ignored) { 64 | final int size = 5; 65 | System.setProperty("com.aparapi.dumpProfilesOnExit", "true"); 66 | boolean verbose = false; 67 | if (verbose) 68 | { 69 | System.setProperty("com.aparapi.enableVerboseJNI", "true"); 70 | System.setProperty("com.aparapi.dumpFlags", "true"); 71 | System.setProperty("com.aparapi.enableShowGeneratedOpenCL", "true"); 72 | System.setProperty("com.aparapi.enableVerboseJNIOpenCLResourceTracking", "true"); 73 | System.setProperty("com.aparapi.enableExecutionModeReporting", "true"); 74 | } 75 | 76 | System.out.println(KernelManager.instance().bestDevice()); 77 | 78 | int[] argbs = testImage.getRGB(0, 0, testImage.getWidth(), testImage.getHeight(), null, 0, testImage.getWidth()); 79 | MedianKernel7x7 kernel = createMedianKernel(argbs); 80 | 81 | kernel.processImages(new MedianSettings(size)); 82 | BufferedImage out = new BufferedImage(testImage.getWidth(), testImage.getHeight(), BufferedImage.TYPE_INT_RGB); 83 | out.setRGB(0, 0, testImage.getWidth(), testImage.getHeight(), kernel._destPixels, 0, testImage.getWidth()); 84 | ImageIcon icon1 = new ImageIcon(testImage); 85 | JLabel label1 = new JLabel(icon1); 86 | ImageIcon icon2 = new ImageIcon(out); 87 | JLabel label2 = new JLabel(icon2); 88 | JFrame frame = new JFrame("Test Median"); 89 | frame.setLayout(new FlowLayout()); 90 | frame.getContentPane().add(label1); 91 | frame.getContentPane().add(label2); 92 | frame.pack(); 93 | frame.setLocationRelativeTo(null); 94 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 95 | frame.setVisible(true); 96 | 97 | StringBuilder builder = new StringBuilder(); 98 | KernelManager.instance().reportDeviceUsage(builder, true); 99 | System.out.println(builder); 100 | 101 | int reps = 50; 102 | final boolean newKernel = false; 103 | for (int rep = 0; rep < reps; ++rep) { 104 | if (newKernel) { 105 | kernel.dispose(); 106 | kernel = createMedianKernel(argbs); 107 | } 108 | long start = System.nanoTime(); 109 | kernel.processImages(new MedianSettings(size)); 110 | long elapsed = System.nanoTime() - start; 111 | System.out.println("elapsed = " + elapsed / 1000000f + "ms"); 112 | } 113 | 114 | builder = new StringBuilder(); 115 | KernelManager.instance().reportDeviceUsage(builder, true); 116 | System.out.println(builder); 117 | } 118 | 119 | private static MedianKernel7x7 createMedianKernel(int[] argbs) { 120 | MedianKernel7x7 kernel = new MedianKernel7x7(); 121 | kernel._imageTypeOrdinal = MedianKernel7x7.RGB; 122 | kernel._sourceWidth = testImage.getWidth(); 123 | kernel._sourceHeight = testImage.getHeight(); 124 | kernel._sourcePixels = argbs; 125 | kernel._destPixels = new int[argbs.length]; 126 | return kernel; 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/median/MedianSettings.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.median; 27 | 28 | /** 29 | *

MedianSettings class.

30 | * 31 | * @author freemo 32 | * @version $Id: $Id 33 | */ 34 | public class MedianSettings { 35 | public final int windowWidth; 36 | public final int windowHeight; 37 | 38 | /** 39 | *

Constructor for MedianSettings.

40 | * 41 | * @param windowSize a int. 42 | */ 43 | public MedianSettings(int windowSize) { 44 | this(windowSize, windowSize); 45 | } 46 | 47 | /** 48 | *

Constructor for MedianSettings.

49 | * 50 | * @param windowWidth a int. 51 | * @param windowHeight a int. 52 | */ 53 | public MedianSettings(int windowWidth, int windowHeight) { 54 | this.windowWidth = windowWidth; 55 | this.windowHeight = windowHeight; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/oopnbody/Body.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.oopnbody; 27 | 28 | 29 | /** 30 | *

Body class.

31 | * 32 | * @author freemo 33 | * @version $Id: $Id 34 | */ 35 | public final class Body{ 36 | /** Constant allBodies */ 37 | public static Body[] allBodies; 38 | 39 | final private boolean isHeavy; 40 | 41 | /** 42 | *

Constructor for Body.

43 | * 44 | * @param x a float. 45 | * @param y a float. 46 | * @param z a float. 47 | * @param _m a float. 48 | * @param isHeavy a boolean. 49 | */ 50 | public Body(float x, float y, float z, float _m, boolean isHeavy) { 51 | this.x = x; 52 | this.y = y; 53 | this.z = z; 54 | m = _m; 55 | this.isHeavy = isHeavy; 56 | } 57 | 58 | float x, y, z, m, vx, vy, vz; 59 | 60 | /** 61 | *

isHeavy.

62 | * 63 | * @return a boolean. 64 | */ 65 | public boolean isHeavy() { 66 | return isHeavy; 67 | } 68 | 69 | /** 70 | *

Getter for the field x.

71 | * 72 | * @return a float. 73 | */ 74 | public float getX() { 75 | return x; 76 | } 77 | 78 | /** 79 | *

Getter for the field y.

80 | * 81 | * @return a float. 82 | */ 83 | public float getY() { 84 | return y; 85 | } 86 | 87 | /** 88 | *

Getter for the field z.

89 | * 90 | * @return a float. 91 | */ 92 | public float getZ() { 93 | return z; 94 | } 95 | 96 | /** 97 | *

getRadius.

98 | * 99 | * @return a float. 100 | */ 101 | public float getRadius() { 102 | return (float) Math.sqrt(x * x + y * y + z* z); 103 | } 104 | 105 | /** 106 | *

getTheta.

107 | * 108 | * @return a float. 109 | */ 110 | public float getTheta() { 111 | return (float) Math.acos(z / getRadius()); 112 | } 113 | 114 | /** 115 | *

getPhi.

116 | * 117 | * @return a float. 118 | */ 119 | public float getPhi() { 120 | return (float) Math.atan(y / x); 121 | } 122 | 123 | /** 124 | *

Getter for the field vx.

125 | * 126 | * @return a float. 127 | */ 128 | public float getVx() { 129 | return vx; 130 | } 131 | 132 | /** 133 | *

Getter for the field vy.

134 | * 135 | * @return a float. 136 | */ 137 | public float getVy() { 138 | return vy; 139 | } 140 | 141 | /** 142 | *

Getter for the field vz.

143 | * 144 | * @return a float. 145 | */ 146 | public float getVz() { 147 | return vz; 148 | } 149 | 150 | /** 151 | *

Getter for the field m.

152 | * 153 | * @return a float. 154 | */ 155 | public float getM() { 156 | return m; 157 | } 158 | 159 | /** 160 | *

Setter for the field m.

161 | * 162 | * @param _m a float. 163 | */ 164 | public void setM(float _m) { 165 | m = _m; 166 | } 167 | 168 | /** 169 | *

Setter for the field x.

170 | * 171 | * @param _x a float. 172 | */ 173 | public void setX(float _x) { 174 | x = _x; 175 | } 176 | 177 | /** 178 | *

Setter for the field y.

179 | * 180 | * @param _y a float. 181 | */ 182 | public void setY(float _y) { 183 | y = _y; 184 | } 185 | 186 | /** 187 | *

Setter for the field z.

188 | * 189 | * @param _z a float. 190 | */ 191 | public void setZ(float _z) { 192 | z = _z; 193 | } 194 | 195 | /** 196 | *

setRadius.

197 | * 198 | * @param radius a float. 199 | */ 200 | public void setRadius(float radius) { 201 | final float theta = getTheta(); 202 | final float phi = getPhi(); 203 | x = (float) (radius * Math.cos(theta) * Math.sin(phi)); 204 | y = (float) (radius * Math.sin(theta) * Math.sin(phi)); 205 | z = (float) (radius * Math.cos(phi)); 206 | } 207 | 208 | /** 209 | *

setTheta.

210 | * 211 | * @param theta a float. 212 | */ 213 | public void setTheta(float theta) { 214 | final float radius = getRadius(); 215 | final float phi = getPhi(); 216 | x = (float) (radius * Math.cos(theta) * Math.sin(phi)); 217 | y = (float) (radius * Math.sin(theta) * Math.sin(phi)); 218 | z = (float) (radius * Math.cos(phi)); 219 | } 220 | 221 | /** 222 | *

setPhi.

223 | * 224 | * @param phi a float. 225 | */ 226 | public void setPhi(float phi) { 227 | final float radius = getRadius(); 228 | final float theta = getTheta(); 229 | x = (float) (radius * Math.cos(theta) * Math.sin(phi)); 230 | y = (float) (radius * Math.sin(theta) * Math.sin(phi)); 231 | z = (float) (radius * Math.cos(phi)); 232 | } 233 | 234 | /** 235 | *

Setter for the field vx.

236 | * 237 | * @param _vx a float. 238 | */ 239 | public void setVx(float _vx) { 240 | vx = _vx; 241 | } 242 | 243 | /** 244 | *

Setter for the field vy.

245 | * 246 | * @param _vy a float. 247 | */ 248 | public void setVy(float _vy) { 249 | vy = _vy; 250 | } 251 | 252 | /** 253 | *

Setter for the field vz.

254 | * 255 | * @param _vz a float. 256 | */ 257 | public void setVz(float _vz) { 258 | vz = _vz; 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/progress/LongRunningKernel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.progress; 27 | 28 | import com.aparapi.Kernel; 29 | 30 | /** 31 | * Kernel which performs very many meaningless calculations, used to demonstrate progress tracking and cancellation of multi-pass Kernels. 32 | * 33 | * @author freemo 34 | * @version $Id: $Id 35 | */ 36 | public class LongRunningKernel extends Kernel { 37 | 38 | /** Constant RANGE=20000 */ 39 | public static final int RANGE = 20000; 40 | private static final int REPETITIONS = 1 * 1000 * 1000; 41 | 42 | public final long[] data = new long[RANGE]; 43 | 44 | /** {@inheritDoc} */ 45 | @Override 46 | public void run() { 47 | int id = getGlobalId(); 48 | if (id == 0) { 49 | report(); 50 | } 51 | for (int rep = 0; rep < REPETITIONS; ++rep) { 52 | data[id] += (int) sqrt(1); 53 | } 54 | } 55 | 56 | /** 57 | *

report.

58 | */ 59 | @NoCL 60 | public void report() { 61 | int passId = getPassId(); 62 | System.out.println("Java execution: passId = " + passId); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/progress/MultiPassKernelSwingWorkerDemo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | package com.aparapi.examples.progress; 27 | 28 | import com.aparapi.*; 29 | import com.aparapi.internal.kernel.*; 30 | import com.aparapi.util.swing.MultiPassKernelSwingWorker; 31 | 32 | import javax.swing.*; 33 | import javax.swing.plaf.nimbus.NimbusLookAndFeel; 34 | import java.awt.*; 35 | import java.awt.event.ActionEvent; 36 | import java.awt.event.ActionListener; 37 | 38 | /** 39 | * Demonstrates progress tracking and cancellation for multi-pass kernels, via {@link com.aparapi.util.swing.MultiPassKernelSwingWorker}. 40 | * 41 | * @author freemo 42 | * @version $Id: $Id 43 | */ 44 | public class MultiPassKernelSwingWorkerDemo { 45 | 46 | private static final int PASS_COUNT = 200; 47 | private static JButton startButton; 48 | private static JButton cancelButton; 49 | private static JProgressBar progress; 50 | private static JLabel status = new JLabel("Press Start", JLabel.CENTER); 51 | private static LongRunningKernel kernel; 52 | private static MultiPassKernelSwingWorker worker; 53 | 54 | private static final boolean TEST_JTP = false; 55 | 56 | /** 57 | *

main.

58 | * 59 | * @param ignored an array of {@link java.lang.String} objects. 60 | * @throws java.lang.Exception if any. 61 | */ 62 | public static void main(String[] ignored) throws Exception { 63 | if (TEST_JTP) { 64 | KernelManager.setKernelManager(KernelManagers.JTP_ONLY); 65 | } 66 | kernel = new LongRunningKernel(); 67 | 68 | UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName()); 69 | JPanel rootPanel = new JPanel(); 70 | rootPanel.setLayout(new BorderLayout()); 71 | JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER)); 72 | startButton = new JButton("Start"); 73 | cancelButton = new JButton("Cancel"); 74 | startButton.setEnabled(true); 75 | startButton.addActionListener(new ActionListener() { 76 | @Override 77 | public void actionPerformed(ActionEvent e) { 78 | start(); 79 | } 80 | }); 81 | cancelButton.setEnabled(false); 82 | cancelButton.addActionListener(new ActionListener() { 83 | @Override 84 | public void actionPerformed(ActionEvent e) { 85 | cancel(); 86 | } 87 | }); 88 | buttons.add(startButton); 89 | buttons.add(cancelButton); 90 | rootPanel.add(buttons, BorderLayout.SOUTH); 91 | 92 | progress = new JProgressBar(new DefaultBoundedRangeModel(0, 0, 0, PASS_COUNT)); 93 | 94 | rootPanel.add(status, BorderLayout.CENTER); 95 | rootPanel.add(progress, BorderLayout.NORTH); 96 | 97 | JFrame frame = new JFrame("MultiPassKernelSwingWorker Demo"); 98 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 99 | frame.getContentPane().add(rootPanel); 100 | frame.pack(); 101 | frame.setLocationRelativeTo(null); 102 | frame.setVisible(true); 103 | } 104 | 105 | private static MultiPassKernelSwingWorker createWorker() { 106 | return new MultiPassKernelSwingWorker(kernel) { 107 | @Override 108 | protected void executeKernel(Kernel kernel) { 109 | int range; 110 | if (TEST_JTP) { 111 | range = LongRunningKernel.RANGE / 1000; 112 | } else { 113 | range = LongRunningKernel.RANGE; 114 | } 115 | kernel.execute(range, PASS_COUNT); 116 | } 117 | 118 | @Override 119 | protected void updatePassId(int passId) { 120 | updateProgress(passId); 121 | } 122 | 123 | @Override 124 | protected void done() { 125 | updateProgress(KernelRunner.PASS_ID_COMPLETED_EXECUTION); 126 | startButton.setEnabled(true); 127 | cancelButton.setEnabled(false); 128 | } 129 | }; 130 | } 131 | 132 | private static void start() { 133 | if (!SwingUtilities.isEventDispatchThread()) { 134 | throw new IllegalStateException(); 135 | } 136 | 137 | startButton.setEnabled(false); 138 | cancelButton.setEnabled(true); 139 | worker = createWorker(); 140 | worker.execute(); 141 | System.out.println("Started execution of MultiPassKernelSwingWorker"); 142 | } 143 | 144 | private static void updateProgress(int passId) { 145 | int progressValue; 146 | if (passId >= 0) { 147 | progressValue = passId; 148 | status.setText("passId = " + passId); 149 | } else if (passId == KernelRunner.PASS_ID_PREPARING_EXECUTION) { 150 | progressValue = 0; 151 | status.setText("Preparing"); 152 | } else if (passId == KernelRunner.PASS_ID_COMPLETED_EXECUTION) { 153 | progressValue = PASS_COUNT; 154 | status.setText("Complete"); 155 | } else { 156 | progressValue = 0; 157 | status.setText("Illegal status " + passId); 158 | } 159 | progress.getModel().setValue(progressValue); 160 | } 161 | 162 | private static void cancel() { 163 | worker.cancelExecution(); 164 | } 165 | } 166 | 167 | -------------------------------------------------------------------------------- /src/main/java/com/aparapi/examples/squares/Main.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 - 2018 Syncleus, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * This product currently only contains code developed by authors 18 | * of specific components, as identified by the source code files. 19 | * 20 | * Since product implements StAX API, it has dependencies to StAX API 21 | * classes. 22 | * 23 | * For additional credits (generally to people who reported problems) 24 | * see CREDITS file. 25 | */ 26 | /* 27 | Copyright (c) 2010-2011, Advanced Micro Devices, Inc. 28 | All rights reserved. 29 | 30 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 31 | following conditions are met: 32 | 33 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following 34 | disclaimer. 35 | 36 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 37 | disclaimer in the documentation and/or other materials provided with the distribution. 38 | 39 | Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products 40 | derived from this software without specific prior written permission. 41 | 42 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 43 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 46 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 47 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 48 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 49 | 50 | If you use the software (in whole or in part), you shall adhere to all applicable U.S., European, and other export 51 | laws, including but not limited to the U.S. Export Administration Regulations ("EAR"), (15 C.F.R. Sections 730 through 52 | 774), and E.U. Council Regulation (EC) No 1334/2000 of 22 June 2000. Further, pursuant to Section 740.6 of the EAR, 53 | you hereby certify that, except pursuant to a license granted by the United States Department of Commerce Bureau of 54 | Industry and Security or as otherwise permitted pursuant to a License Exception under the U.S. Export Administration 55 | Regulations ("EAR"), you will not (1) export, re-export or release to a national of a country in Country Groups D:1, 56 | E:1 or E:2 any restricted technology, software, or source code you receive hereunder, or (2) export to Country Groups 57 | D:1, E:1 or E:2 the direct product of such technology or software, if such foreign produced direct product is subject 58 | to national security controls as identified on the Commerce Control List (currently found in Supplement 1 to Part 774 59 | of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations 60 | under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/. 61 | 62 | */ 63 | 64 | package com.aparapi.examples.squares; 65 | 66 | import com.aparapi.Kernel; 67 | import com.aparapi.Range; 68 | 69 | /** 70 | * An example Aparapi application which computes and displays squares of a set of 512 input values. 71 | * While executing on GPU using Aparpi framework, each square value is computed in a separate kernel invocation and 72 | * can thus maximize performance by optimally utilizing all GPU computing units 73 | * 74 | * @author gfrost 75 | * @version $Id: $Id 76 | */ 77 | public class Main{ 78 | 79 | /** 80 | *

main.

81 | * 82 | * @param _args an array of {@link java.lang.String} objects. 83 | */ 84 | public static void main(String[] _args) { 85 | 86 | final int size = 512; 87 | 88 | /** Input float array for which square values need to be computed. */ 89 | final float[] values = new float[size]; 90 | 91 | /** Initialize input array. */ 92 | for (int i = 0; i < size; i++) { 93 | values[i] = i; 94 | } 95 | 96 | /** Output array which will be populated with square values of corresponding input array elements. */ 97 | final float[] squares = new float[size]; 98 | 99 | /** Aparapi Kernel which computes squares of input array elements and populates them in corresponding elements of 100 | * output array. 101 | **/ 102 | Kernel kernel = new Kernel(){ 103 | @Override public void run() { 104 | int gid = getGlobalId(); 105 | squares[gid] = values[gid] * values[gid]; 106 | } 107 | }; 108 | 109 | // Execute Kernel. 110 | 111 | kernel.execute(Range.create(512)); 112 | 113 | // Report target execution mode: GPU or JTP (Java Thread Pool). 114 | System.out.println("Device = " + kernel.getTargetDevice().getShortDescription()); 115 | 116 | // Display computed square values. 117 | for (int i = 0; i < size; i++) { 118 | System.out.printf("%6.0f %8.0f\n", values[i], squares[i]); 119 | } 120 | 121 | // Dispose Kernel resources. 122 | kernel.dispose(); 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /src/main/resources/convolution.cl: -------------------------------------------------------------------------------- 1 | void processPixel(__global float* _convMatrix3x3, __global char* _imageIn, __global char* _imageOut, int _width, int _height, int _x, int _y){ 2 | float accum = 0.0f; 3 | int count = 0; 4 | for (int dx = -3; dx<6; dx+=3){ 5 | for (int dy = -1; dy<2; dy++){ 6 | int rgb = 0xff & _imageIn[(((_y + dy) * _width) + (_x + dx))]; 7 | accum = accum + ((float)rgb * _convMatrix3x3[count++]); 8 | } 9 | } 10 | char value = (char )max(0, min((int)accum, 255)); 11 | _imageOut[(_y * _width) + _x] = value; 12 | return; 13 | } 14 | 15 | __kernel void applyConvolution( 16 | __global float *_convMatrix3x3, // only read from kernel 17 | __global char *_imageIn, // only read from kernel 18 | __global char *_imageOut, // only written to (never read) from kernel 19 | int _width, 20 | int _height 21 | ){ 22 | int x = get_global_id(0) % (_width * 3); 23 | int y = get_global_id(0) / (_width * 3); 24 | if (x>3 && x<((_width * 3) - 3) && y>1 && y<(_height - 1)){ 25 | processPixel(_convMatrix3x3, _imageIn, _imageOut, _width*3, _height, x, y); 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /src/main/resources/duke.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syncleus/aparapi-examples/66a48dc5b4f0f81640cb6be3046426bb02860734/src/main/resources/duke.jpg -------------------------------------------------------------------------------- /src/main/resources/knight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syncleus/aparapi-examples/66a48dc5b4f0f81640cb6be3046426bb02860734/src/main/resources/knight.png -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Root logger option 2 | log4j.rootLogger=DEBUG, stdout 3 | 4 | # Direct log messages to stdout 5 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 6 | log4j.appender.stdout.Target=System.out 7 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n -------------------------------------------------------------------------------- /src/main/resources/mandel.cl: -------------------------------------------------------------------------------- 1 | #define MAX_ITERATIONS 64 2 | 3 | __constant const int pallette[]={ 4 | -65536, 5 | -59392, 6 | -53248, 7 | -112640, 8 | -106752, 9 | -166144, 10 | -160256, 11 | -219904, 12 | -279552, 13 | -339200, 14 | -399104, 15 | -985344, 16 | -2624000, 17 | -4197376, 18 | -5770496, 19 | -7343872, 20 | -8851712, 21 | -10425088, 22 | -11932928, 23 | -13375232, 24 | -14817792, 25 | -16260096, 26 | -16719602, 27 | -16720349, 28 | -16721097, 29 | -16721846, 30 | -16722595, 31 | -16723345, 32 | -16724351, 33 | -16725102, 34 | -16726110, 35 | -16727119, 36 | -16728129, 37 | -16733509, 38 | -16738889, 39 | -16744269, 40 | -16749138, 41 | -16754006, 42 | -16758619, 43 | -16762976, 44 | -16767077, 45 | -16771178, 46 | -16774767, 47 | -16514932, 48 | -15662970, 49 | -14942079, 50 | -14221189, 51 | -13631371, 52 | -13107088, 53 | -12648342, 54 | -12320669, 55 | -11992995, 56 | -11796393, 57 | -11665328, 58 | -11993019, 59 | -12386248, 60 | -12845011, 61 | -13303773, 62 | -13762534, 63 | -14286830, 64 | -14745588, 65 | -15269881, 66 | -15728637, 67 | -16252927, 68 | 0 69 | }; 70 | 71 | __kernel void createMandleBrot( 72 | float scale, 73 | float offsetx, 74 | float offsety, 75 | __global int *rgb 76 | ){ 77 | int gid = get_global_id(0) + get_global_id(1)*get_global_size(0); 78 | float x = ((((float)(get_global_id(0)) * scale) - ((scale / 2.0f) * (float)get_global_size(0))) / (float)get_global_size(0)) + offsetx; 79 | float y = ((((float)(get_global_id(1)) * scale) - ((scale / 2.0f) * (float)get_global_size(1))) / (float)get_global_size(1)) + offsety; 80 | int count = 0; 81 | float zx = x; 82 | float zy = y; 83 | float new_zx = 0.0f; 84 | for (; count