<commit_before>/**
 * Copyright (c) 2016 - 2017 Syncleus, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aparapi.runtime;

import static org.junit.Assert.assertArrayEquals;

import com.aparapi.Kernel;
import com.aparapi.Range;
import com.aparapi.device.Device;
import com.aparapi.device.OpenCLDevice;
import com.aparapi.internal.kernel.KernelManager;
import static org.junit.Assume.*;
import org.junit.BeforeClass;
import org.junit.Test;

public class LocalArrayArgsIssue79Test {
    static OpenCLDevice openCLDevice = null;
    private static final int SIZE = 32;
    private int[] targetArray;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        Device device = KernelManager.instance().bestDevice();
        assumeTrue (device != null && device instanceof OpenCLDevice);
        openCLDevice = (OpenCLDevice) device;
    }

    @Test
    public void test() {
        final LocalArrayArgsKernel kernel = new LocalArrayArgsKernel();
        final Range range = openCLDevice.createRange(SIZE, SIZE);
        targetArray = new int[SIZE];
        kernel.setExplicit(false);
        kernel.setArray(targetArray);
        kernel.execute(range);
        validate();
    }
    
    @Test
    public void testExplicit() {
        final LocalArrayArgsKernel kernel = new LocalArrayArgsKernel();
        final Range range = openCLDevice.createRange(SIZE, SIZE);
        targetArray = new int[SIZE];
        kernel.setExplicit(true);
        kernel.setArray(targetArray);
        kernel.put(targetArray);
        kernel.execute(range);
        kernel.get(targetArray);
        validate();
    }

    void validate() {
        int[] expected = new int[SIZE];
        for (int threadId = 0; threadId < SIZE; threadId++) {
            for (int i = 0; i < SIZE; i++) {
                expected[threadId] += i + threadId;
            }
        	expected[threadId] *= threadId;
        }
        
        assertArrayEquals("destArray", expected, targetArray);
    }

    public static class LocalArrayArgsKernel extends Kernel {
        private int[] destArray;
        
        @Local
        private int[] myArray = new int[SIZE];

        public LocalArrayArgsKernel() {
        }
        
        @NoCL
        public void setArray(int[] target) {
            destArray = target;
        }

        private void doComputation1(@Local int[] arr, int id) {
            for (int i = 0; i < SIZE; i++) {
                arr[id] += i + id;
            }
        }

        private void doComputation2(int[] arr_$local$, int id) {
            arr_$local$[id] *= id;
        }

        @Override
        public void run() {
            int id = getLocalId();
            
            myArray[id] = destArray[id];

            doComputation1(myArray, id);
            doComputation2(myArray, id);
            
            destArray[id] = myArray[id];            
        }
    }
}<commit_msg>Update: Add support for Local arguments in kernel functions (refs #79) - improved unit test<commit_after>/**
 * Copyright (c) 2016 - 2017 Syncleus, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aparapi.runtime;

import static org.junit.Assert.assertArrayEquals;

import com.aparapi.Kernel;
import com.aparapi.Range;
import com.aparapi.device.Device;
import com.aparapi.device.OpenCLDevice;
import com.aparapi.internal.kernel.KernelManager;
import static org.junit.Assume.*;

import org.junit.Before;
import org.junit.Test;

public class LocalArrayArgsIssue79Test {
    static OpenCLDevice openCLDevice = null;
    private static final int SIZE = 32;
    private int[] targetArray;

    @Before
    public void setUpBeforeClass() throws Exception {
        Device device = KernelManager.instance().bestDevice();
        assumeTrue (device != null && device instanceof OpenCLDevice);
        openCLDevice = (OpenCLDevice) device;
    }

    @Test
    public void test() {
        final LocalArrayArgsKernel kernel = new LocalArrayArgsKernel();
        final Range range = openCLDevice.createRange(SIZE, SIZE);
        targetArray = new int[SIZE];
        kernel.setExplicit(false);
        kernel.setArray(targetArray);
        kernel.execute(range);
        validate();
    }
    
    @Test
    public void testExplicit() {
        final LocalArrayArgsKernel kernel = new LocalArrayArgsKernel();
        final Range range = openCLDevice.createRange(SIZE, SIZE);
        targetArray = new int[SIZE];
        kernel.setExplicit(true);
        kernel.setArray(targetArray);
        kernel.put(targetArray);
        kernel.execute(range);
        kernel.get(targetArray);
        validate();
    }

    void validate() {
        int[] expected = new int[SIZE];
        for (int threadId = 0; threadId < SIZE; threadId++) {
            for (int i = 0; i < SIZE; i++) {
                expected[threadId] += i + threadId;
            }
        	expected[threadId] *= threadId;
        }
        
        assertArrayEquals("targetArray", expected, targetArray);
    }

    public static class LocalArrayArgsKernel extends Kernel {
        private int[] resultArray;
        
        @Local
        private int[] myArray = new int[SIZE];
        
        @PrivateMemorySpace(SIZE)
        private int[] other_$private$ = new int[SIZE];

        public LocalArrayArgsKernel() {
        }
        
        @NoCL
        public void setArray(int[] target) {
            resultArray = target;
        }

        private void doInitialCopy(@Local int[] target, int[] source, int id) {
        	target[id] = source[id];
        }
        
        private void doComputation1(@Local int[] arr, int id) {
            for (int i = 0; i < SIZE; i++) {
                arr[id] += i + id;
            }
        }

        private void doComputation2(int[] arr_$local$, int id) {
            arr_$local$[id] *= id;
        }
        
        private void doComputation3(int[] arr_$local$, int[] arr_$private$, int id) {
        	arr_$private$[id] = arr_$local$[id];
        }

        @Override
        public void run() {
            int id = getLocalId();                
            
            
            doInitialCopy(myArray, resultArray, id);
            doComputation1(myArray, id);
            doComputation2(myArray, id);
            doComputation3(myArray, other_$private$, id);
            
            resultArray[id] = myArray[id];            
        }
    }
}
{'fraction_non_alphanumeric': 0.09948842218632202, 'fraction_numerical': 0.006731287022078622, 'mean_word_length': 2.2640597539543057, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555042', 'n_tokens_mistral': 2072, 'n_tokens_neox': 2003, 'n_words': 599}