git-svn-id: http://svn.code.sf.net/p/petavision/code/trunk@2795 7c0444ca-5550-0410-9008-faecdbe78eb5

//#include <OpenCL/opencl.h>

#include <stdio.h>

//
// simple compute kernel
//

//
//// 4. change if () guard so that it uses kx and ky

__kernel void bandwidth (
    __global float * V,
    __global float * G_E,
    __global float * phiExc,
    __global float * G_I,
    __global float * phiInh,
    __global float * G_IB,
    __global float * phiInhB,
    const uint nx,
    const uint ny,
    const uint nPad)
{
   float tauInf, VmemInf;
   float expExcite, expInhib, expInhibB;
   float dt, tau;
   float Vrest, Vexc, Vinh, VinhB;

   //ADD DT, HARD CODE IN VARIABLES, FIX stuff, implement and check by hand, and check boundaries

   const uint kx = get_global_id(0);
   const uint ky = get_global_id(1);
   const uint k  = kx + nx*ky;

   float one = 1.1f;

   VmemInf = 20.0f;
   //tauInf = 3.141f;

   //Hard Coded Variables
   tau = 20.0;
   Vrest = -70.0;
   Vexc = 0.0;
   Vinh = -75.0;
   VinhB = -90.0;

   dt = 1.0f;
   tau = 1.0f;
   Vrest = 2.0f;
   Vexc = 2.0f;
   Vinh = 2.0f;
   VinhB = 2.0f;

   expExcite = 1.0f;
   expInhib = 2.0f;
   expInhibB = 2.0f;

   if (kx < 4096 && ky < 4096) {         //(k < nx*ny) {
      //printf ("%f %f %f\n", kx, ky, k);
      G_E[k]  = 1 + G_E[k] ;phiExc[k] + G_E[k]  * expExcite;
      G_I[k]  = phiInh[k]; //phiInh[k] + G_I[k]  * expInhib;
      G_IB[k] = phiInhB[k] + G_IB[k] * expInhibB;
      tauInf  = (dt / tau) * (1 + G_E[k] + G_I[k] + G_IB[k]);
      VmemInf = ( Vrest + G_E[k] * Vexc + G_I[k] * Vinh + G_IB[k] * VinhB )
                / (1.0 + G_E[k] + G_I[k] + G_IB[k]);
      V[k] = VmemInf + (V[k] - VmemInf) * exp(-tauInf);

      //printf("G_I[%d]=%f\n", k, G_I[k]);
      //printf("kx=%f\n", kx);
      //printf("nx=%f\n", nx);

//      V[k] = 8.0f;
   }
}

error: pathspec 'src/layers/mem.cl' did not match any file(s) known to git


Create raytracer.cl

Create raytracer.cl

//
//  raytracer.cl
//  OpenCL_Raytracer
//
//  Created by Stefan Matthes on 18.07.15.
//
//

////////////////////////////////////////////////////////////////////////////////

#ifndef RAYTRACER_KERNEL
#define RAYTRACER_KERNEL

#define SAMPLE_COUNT_AMBIENT 1
#define SAMPLE_COUNT_AMBIENT_FACTOR (1.f/SAMPLE_COUNT_AMBIENT)
#define SAMPLE_COUNT_DIRECT 2
#define SAMPLE_COUNT_DIRECT_FACTOR (1.f/SAMPLE_COUNT_DIRECT)
#define FAR_CLIPPING_PLANE 100.f
#define RAYTRACING_DEPTH 3
#define MAX_PENDING_QBVH_NODES 32

typedef struct QuadNode
{
    float4 bbox[2*3];
    unsigned int child_nodes[4];
    unsigned int number_of_spheres[4];
}QuadNode;

typedef struct Sphere
{
    float4 pos_and_r;
    float4 color;
    unsigned int texture_index;
    float reflection;
    float refraction;
    float eta;
}Sphere;

////////////////////////////////////////////////////////////////////////////////

float getRandomPixelOffset(unsigned int *seed);

float4 getRandomVector(unsigned int *seed);

float4 reflect(const float4 incident_ray, const float4 surface_normal);

float4 refract(const float4 incident_ray, const float4 surface_normal, const float eta);

float4 refractThroughSphere(const float4 incident_ray, const float4 surface_normal, float4* intersection_point, const float4 sphere_pos, const float radius, const float eta);

bool raySphereIntersection(const Sphere sphere, const float4 ray_origin, const float4 ray_direction, const float max_distance, float* distance);

unsigned int rayQBoxIntersection(const float4 ray_origin, const float4 ray_direction, const float4* bbox, float max_distance, unsigned int* child_nodes);

bool lightOcclusion(const __global Sphere* sphere_buffer, const unsigned int sphere_count, const unsigned int self, const float4 point, const float4 light_direction, float light_distance);

float4 lightIntensity(const __global Sphere* sphere_buffer, const unsigned int sphere_count, const unsigned int self, const float4 point, const float4 normal, const Sphere light, unsigned int *seed, const float ambient_lighting);

float ambientLighting(const __global Sphere* sphere_buffer, const unsigned int sphere_count, const unsigned int self, const float4 point, const float4 normal, unsigned int seed);

float4 directIllumination(const __global Sphere* sphere_buffer, const unsigned int sphere_count, const unsigned int first_light_index, const unsigned int self, const float4 point, const float4 normal, unsigned int* seed);

unsigned int trace(const __global QuadNode* qbvh_buffer, const __global Sphere* sphere_buffer, const unsigned int sphere_count, const float4 ray_origin, const float4 ray_direction, unsigned int *index, float *distance);

float4 rayDirection(const __global float* cam_pose, const float x, const float y);

////////////////////////////////////////////////////////////////////////////////

float getRandomPixelOffset(unsigned int *seed)
{
    const unsigned int a = 48171;
    const unsigned int m = 1<<16;
    const float scaling_factor = 1.0f/float(1<<16 - 1);
    float offset = float(*seed)*scaling_factor - 0.5f;
    //const float scaling_factor = 1.0f/float(1<<16 - 1);
    //float offset = float(*seed)*scaling_factor - 0.5f;

    *seed = (a * *seed) % m;
    return offset;
}

float4 getRandomVector(unsigned int *seed)
{
    const unsigned int a = 48171;
    const unsigned int m = 1<<16;
    const float scaling_factor = 2.f/float(1<<16 - 1);

    float4 random_ray;

    random_ray.x = float(*seed);
    *seed = (a * *seed) % m;
    random_ray.y = float(*seed);
    *seed = (a * *seed) % m;
    random_ray.z = float(*seed);
    *seed = (a * *seed) % m;

    random_ray = random_ray*scaling_factor - 1.f;
    random_ray.w = 0.f;

    return normalize(random_ray);
}

float4 reflect(const float4 incident_ray, const float4 surface_normal)
{
    return incident_ray - 2.f*dot(surface_normal, incident_ray) * surface_normal;
}

float4 refract(const float4 incident_ray, const float4 surface_normal, const float eta)
{
    float4 refracted_ray;

    float n_dot_i = dot(surface_normal, incident_ray);
    float lambda = 1.f - eta * eta * (1.f - n_dot_i * n_dot_i);

    if(lambda >= 0.f)
    refracted_ray = eta * incident_ray - surface_normal * (eta * n_dot_i + sqrt(lambda));

    refracted_ray.w = 0.f;
    return refracted_ray;
}

float4 refractThroughSphere(const float4 incident_ray,
                            const float4 surface_normal,
                            float4* intersection_point,
                            const float4 sphere_pos,
                            const float radius,
                            const float eta)
{
    float4 refracted_ray = refract(incident_ray, surface_normal, 1.f/eta);

    float4 L = sphere_pos - *intersection_point;
    L.w = 0.f;
    float LdotRR = dot(L, refracted_ray);
    float D2 = dot(L, L) - LdotRR * LdotRR;
    float Distance = LdotRR + sqrt(radius * radius - D2);
    *intersection_point += refracted_ray * Distance;

    float4 NewNormal = (sphere_pos - *intersection_point) / radius;
    float4 refracted_ray2 = refract(refracted_ray, NewNormal, eta);

    return refracted_ray2;
}

bool raySphereIntersection(const Sphere sphere,
                           const float4 ray_origin,
                           const float4 ray_direction,
                           const float max_distance,
                           float* distance)
{
    float4 sphere_direction = sphere.pos_and_r - ray_origin;
    sphere_direction.w = 0.f;

    float lambda = dot(sphere_direction, ray_direction);

    if(lambda > 0.f)//sphere is in front of camera
    {
        float distance_squared = dot(sphere_direction,sphere_direction) - lambda*lambda;
        float radius_squared = sphere.pos_and_r.w * sphere.pos_and_r.w;
        if(distance_squared < radius_squared)
        {
            *distance = lambda - sqrt(radius_squared - distance_squared);
            if(*distance >= 0.f && (*distance < max_distance))
            return true;
        }
    }
    return false;
}

unsigned int rayQBoxIntersection(const float4 ray_origin, const float4 ray_direction, const float4* bbox, float max_distance, unsigned int* child_nodes)
{
    float4 dirfrac = 1.f/ray_direction;

    float4 t1 = (bbox[0] - ray_origin.x)*dirfrac.x;
    float4 t2 = (bbox[1] - ray_origin.x)*dirfrac.x;
    float4 t3 = (bbox[2] - ray_origin.y)*dirfrac.y;
    float4 t4 = (bbox[3] - ray_origin.y)*dirfrac.y;
    float4 t5 = (bbox[4] - ray_origin.z)*dirfrac.z;
    float4 t6 = (bbox[5] - ray_origin.z)*dirfrac.z;

    float4 tmin = fmax(fmax(fmin(t1, t2), fmin(t3, t4)), fmin(t5, t6));
    float4 tmax = fmin(fmin(fmax(t1, t2), fmax(t3, t4)), fmax(t5, t6));

    int4 comp = isgreater(tmax, 0.f) & isgreater(tmax, tmin) & isless(tmin, max_distance);
    unsigned int intersection_count = 0;

    if( comp.x ) child_nodes[intersection_count++] = 0;
    if( comp.y ) child_nodes[intersection_count++] = 1;
    if( comp.z ) child_nodes[intersection_count++] = 2;
    if( comp.w ) child_nodes[intersection_count++] = 3;

    return intersection_count;
}

bool lightOcclusion(const __global Sphere* sphere_buffer,
                    const unsigned int sphere_count,
                    const unsigned int self,
                    const float4 point,
                    const float4 light_direction,
                    float light_distance)
{
    float distance;
    for(unsigned int i=0; i<sphere_count; i++)
    {
        if(i == self) continue;
        if( raySphereIntersection(sphere_buffer[i], point, light_direction, light_distance, &distance) )
        return true;
    }
    return false;
}

float4 lightIntensity(const __global Sphere* sphere_buffer,
                      const unsigned int sphere_count,
                      const unsigned int self,
                      const float4 point,
                      const float4 normal,
                      const Sphere light,
                      unsigned int *seed,
                      const float ambient_lighting)
{
    float4 light_position = light.pos_and_r + getRandomVector(seed)*light.pos_and_r.w;
    float4 light_direction = light_position - point;
    light_direction.w = 0.f;
    float light_distance_squared = dot(light_direction, light_direction);
    float light_distance = sqrt(light_distance_squared);
    light_direction /= light_distance;

    float attenuation = 0.1f + 0.f*light_distance + 0.1f * light_distance_squared;

    float n_dot_ld = dot(normal, light_direction);
    if(n_dot_ld > 0.f)
    {
        if( lightOcclusion(sphere_buffer, sphere_count, self, point, light_direction, light_distance) == false)
            return light.color * (ambient_lighting*light.color.w + n_dot_ld) / attenuation;
    }

    return light.color * ambient_lighting*light.color.w / attenuation;
}

float ambientLighting(const __global Sphere* sphere_buffer, const unsigned int sphere_count, const unsigned int self, const float4 point, const float4 normal, unsigned int seed)
{
    float ambient_lighting = 0.f;

    for(int i = 0; i < SAMPLE_COUNT_AMBIENT; i++)
    {
        float4 random_ray = getRandomVector(&seed);
        float n_dot_rr = dot(normal, random_ray);

        if(n_dot_rr < 0.f)
        {
            random_ray = -random_ray;
            n_dot_rr = -n_dot_rr;
        }

        float object_distance = 100.f;

        float distance;
        for(unsigned int i=0; i<sphere_count; i++)
        {
            if(i == self) continue;

            if( raySphereIntersection(sphere_buffer[i], point, random_ray, object_distance, &distance) )
            object_distance = distance;
        }

        ambient_lighting += n_dot_rr / (1.f + object_distance*object_distance);
    }

    return 1.f - ambient_lighting * SAMPLE_COUNT_AMBIENT_FACTOR;
}

float4 directIllumination(const __global Sphere* sphere_buffer,
                          const unsigned int sphere_count,
                          const unsigned int first_light_index,
                          const unsigned int self,
                          const float4 point,
                          const float4 normal,
                          unsigned int* seed)
{
    float4 lights_intensity = float4(0.f);
    float ambient_lighting = ambientLighting(sphere_buffer, first_light_index, self, point, normal, *seed);

    for(unsigned int light_index = first_light_index; light_index<sphere_count; light_index++)
    {
        for(int i=0; i<SAMPLE_COUNT_DIRECT; i++)
        {
            lights_intensity += lightIntensity(sphere_buffer, first_light_index, self, point, normal, sphere_buffer[light_index], seed, ambient_lighting);
        }
    }

    return lights_intensity * SAMPLE_COUNT_DIRECT_FACTOR;
}

unsigned int trace(const __global QuadNode* qbvh_buffer,
                   const __global Sphere* sphere_buffer,
                   const unsigned int sphere_count,
                   const float4 ray_origin,
                   const float4 ray_direction,
                   unsigned int *idx,
                   float *max_distance)
{
    unsigned int pending_child_nodes[MAX_PENDING_QBVH_NODES] = {};
    unsigned int child_nodes[4]                              = {};
    unsigned int cur_node                                    = 0;
    unsigned int next_node                                   = 0;
    unsigned int new_intersections                           = 0;
    unsigned int intersection_count                          = 1;
    bool hit                                                 = false;

    while( cur_node < intersection_count )
    {
        QuadNode qbvh = qbvh_buffer[pending_child_nodes[cur_node]];
        new_intersections = rayQBoxIntersection(ray_origin, ray_direction, qbvh.bbox, *max_distance, child_nodes);

        for(unsigned int i=0; i<new_intersections; i++)
        {
            next_node = child_nodes[i];
            if(qbvh.number_of_spheres[next_node] > 0) //test all spheres in this qbvh node
            {
                unsigned int first_sphere_index = qbvh.child_nodes[next_node];
                float distance;
                for(unsigned int i=first_sphere_index; i<first_sphere_index+qbvh.number_of_spheres[next_node]; i++)
                {
                    if( raySphereIntersection(sphere_buffer[i], ray_origin, ray_direction, *max_distance, &distance) )
                    {
                        hit = true;
                        *idx = i;
                        *max_distance = distance;
                    }
                }
            }
            else //add new nodes to check
            {
                pending_child_nodes[intersection_count++] = qbvh.child_nodes[next_node];
            }
        }
        cur_node++;
    }

    return hit;
}

float4 rayDirection(const __global float* cam_pose, const float x, const float y)
{
    //Gnomonical camera model
    /*float focal_length_inv = 0.002f;
     float cx_inv = focal_length_inv*0.5f*(get_global_size(0)-1.f);
     float cy_inv = focal_length_inv*0.5f*(get_global_size(1)-1.f);

     float4 ray;
     ray.x = focal_length_inv*x - cx_inv;
     ray.y = focal_length_inv*y - cy_inv;
     ray.z = 1.f;
     ray.w = 0.f;*/

    //Stereographic camera model
    float focal_length_inv = 0.002f;
    float x_c = x - 0.5f*(get_global_size(0)-1.f);
    float y_c = y - 0.5f*(get_global_size(1)-1.f);
    float radius = sqrt(x_c*x_c + y_c*y_c);
    float theta = 2.f*atan(radius*0.5f*focal_length_inv);
    float ratio = tan(theta)/radius;

    float4 ray = (float4)(ratio * x_c, ratio * y_c, 1.f, 0.f);
    float4 unit_ray = normalize(ray);
    float4 ray_direction = (float4)(cam_pose[0]*unit_ray.x+cam_pose[1]*unit_ray.y+cam_pose[2]*unit_ray.z,
                                    cam_pose[4]*unit_ray.x+cam_pose[5]*unit_ray.y+cam_pose[6]*unit_ray.z,
                                    cam_pose[8]*unit_ray.x+cam_pose[9]*unit_ray.y+cam_pose[10]*unit_ray.z,
                                    0.f);

    return ray_direction;
}

__kernel void raytracer(const __global QuadNode* qbvh_buffer,
                        const __global Sphere* sphere_buffer,
                        const __global unsigned int* sphere_count_buffer,
                        const __global unsigned int* first_light_index_buffer,
                        const __global float* cam_pose,
                        const __global uint* seed_buffer,
                        __write_only image2d_t output)
{
    const unsigned int global_x  = get_global_id(0);
    const unsigned int global_y  = get_global_id(1);
    const unsigned int global_id = global_y*get_global_size(0) + global_x;

    unsigned int seed = seed_buffer[global_id];

    float gx = (float)global_x;
    float gy = (float)global_y;
    float4 total_color = (float4)(0.0f, 0.0f, 0.0f, 0.0f);
    for(float dx=-0.3f; dx<=0.3f; dx+=0.3f)//super sampling (9 rays per pixel)
    {
        for(float dy=-0.3f; dy<=0.3f; dy+=0.3f)
        {
            float4 ray_origin = (float4)(cam_pose[3], cam_pose[7], cam_pose[11], 0.f);
            float4 ray_direction = rayDirection(cam_pose, gx+dx, gy+dy);

            float4 color = (float4)(0.f, 0.f, 0.f, 0.0f);

            float distance = FAR_CLIPPING_PLANE;
            unsigned int sphere_idx;
            const unsigned int sphere_count = *sphere_count_buffer;
            const unsigned int first_light_index = *first_light_index_buffer;

            float factor = 1.f;
            for(int iter=0; iter<RAYTRACING_DEPTH; iter++)
            {
                if( trace(qbvh_buffer, sphere_buffer, sphere_count, ray_origin, ray_direction, &sphere_idx, &distance) )
                {
                    Sphere sphere = sphere_buffer[sphere_idx];
                    if( sphere_idx < first_light_index )
                    {
                        ray_origin += ray_direction*distance;
                        float4 normal = (ray_origin - sphere.pos_and_r)/sphere.pos_and_r.w;
                        normal.w = 0.f;

                        float opaque_factor = 1.f - sphere.reflection - sphere.refraction;
                        if( opaque_factor > 0.f )
                        {
                            color += factor*opaque_factor*sphere.color*directIllumination(sphere_buffer, sphere_count, first_light_index, sphere_idx, ray_origin, normal, &seed);
                        }
                        factor *= sphere.reflection;
                        if(factor > 0.f)
                        {
                            distance = FAR_CLIPPING_PLANE;
                            ray_direction = reflect(ray_direction, normal);
                        }
                        else
                            break;
                    }
                    else
                        color += factor*sphere.color; //light color
                }
                else
                    color += factor*(float4)(0.1f, 0.1f, 0.1f, 0.0f); //background color
            }

            total_color += color; //super sampling
        }
    }
    total_color/=9.f;

    write_imagef(output, (int2)(global_x,global_y), total_color);
}

#endif

error: pathspec 'raytracer.cl' did not match any file(s) known to git


Basic read support for CL_BGRA image format.

/* OpenCL built-in library: read_image()

   Copyright (c) 2013 Ville Korhonen
   Copyright (c) 2014 Felix Bytow
   Copyright (c) 2015 Matias Koskela

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "templates.h"
#include "pocl_image_rw_utils.h"

#if (__clang_major__ == 3) && (__clang_minor__ >= 5)
// Clang 3.5 crashes in case trying to cast to the private pointer,
// adding the global qualifier fixes it. Clang 3.4 crashes if it's
// there. The issue is in SROA.
#define ADDRESS_SPACE global
#else
#define ADDRESS_SPACE
#endif

/* checks if integer coord is out of bounds. If out of bounds: Sets coord in
   bounds and returns false OR populates color with border colour and returns
   true. If in bounds, returns false */
int __pocl_is_out_of_bounds (ADDRESS_SPACE dev_image_t* dev_image, int4 coord,
                             dev_sampler_t* dev_sampler, void *color_)
{
  uint4 *color = (uint4*)color_;
  if(*dev_sampler & CLK_ADDRESS_CLAMP_TO_EDGE)
    {
      if (coord.x >= dev_image->width)
        coord.x = dev_image->width-1;
      if (dev_image->height != 0 && coord.y >= dev_image->height)
        coord.y = dev_image->height-1;
      if (dev_image->depth != 0 && coord.z >= dev_image->depth)
        coord.z = dev_image->depth-1;

      if (coord.x < 0)
        coord.x = 0;
      if (coord.y < 0)
        coord.y = 0;
      if (coord.z < 0)
        coord.z = 0;

      return 0;
    }
  if (*dev_sampler & CLK_ADDRESS_CLAMP)
    {
      if(coord.x >= dev_image->width || coord.x < 0 ||
         coord.y >= dev_image->height || coord.y < 0 ||
         (dev_image->depth != 0 && (coord.z >= dev_image->depth || coord.z <0)))
        {
          (*color)[0] = 0;
          (*color)[1] = 0;
          (*color)[2] = 0;

          if (dev_image->order == CL_A || dev_image->order == CL_INTENSITY ||
              dev_image->order == CL_RA || dev_image->order == CL_ARGB ||
              dev_image->order == CL_BGRA || dev_image->order == CL_RGBA)
            (*color)[3] = 0;
          else
            (*color)[3] = 1;

          return 1;
        }
    }
  return 0;
}

/* Reads a four element pixel from image pointed by integer coords. */
void __pocl_read_pixel (void* color, ADDRESS_SPACE dev_image_t* dev_image, int4 coord)
{

  uint4* color_ptr = (uint4*)color;
  int width = dev_image->width;
  int height = dev_image->height;
  int num_channels = dev_image->num_channels;
  int i = num_channels;
  int elem_size = dev_image->elem_size;
  int const base_index =
    (coord.x + coord.y*width + coord.z*height*width) * num_channels;

  if (dev_image->order == CL_A)
    {
      /* these can be garbage
      (*color_ptr)[0] = 0;
      (*color_ptr)[1] = 0;
      (*color_ptr)[2] = 0;
      */
      if (elem_size == 1)
        (*color_ptr)[3] = ((uchar*)(dev_image->data))[base_index];
      else if (elem_size == 2)
        (*color_ptr)[3] = ((ushort*)(dev_image->data))[base_index];
      else if (elem_size == 4)
        (*color_ptr)[3] = ((uint*)(dev_image->data))[base_index];
      return;
    }

  if (elem_size == 1)
    {
      if(dev_image->order == CL_BGRA)
        {
          (*color_ptr)[0] = ((uchar*)(dev_image->data))[base_index + 2];
          (*color_ptr)[1] = ((uchar*)(dev_image->data))[base_index + 1];
          (*color_ptr)[2] = ((uchar*)(dev_image->data))[base_index + 0];
          (*color_ptr)[3] = ((uchar*)(dev_image->data))[base_index + 3];
        }
      else
        {
          while (i--)
            {
              (*color_ptr)[i] = ((uchar*)(dev_image->data))[base_index + i];
            }
        }
    }
  else if (elem_size == 2)
    {
      if(dev_image->order == CL_BGRA)
        {
          (*color_ptr)[0] = ((ushort*)(dev_image->data))[base_index + 2];
          (*color_ptr)[1] = ((ushort*)(dev_image->data))[base_index + 1];
          (*color_ptr)[2] = ((ushort*)(dev_image->data))[base_index + 0];
          (*color_ptr)[3] = ((ushort*)(dev_image->data))[base_index + 3];
        }
      else
        {
          while (i--)
            {
              (*color_ptr)[i] = ((ushort*)(dev_image->data))[base_index + i];
            }
      }
    }
  else if (elem_size == 4)
    {
      if(dev_image->order == CL_BGRA)
        {
          (*color_ptr)[0] = ((uint*)(dev_image->data))[base_index + 2];
          (*color_ptr)[1] = ((uint*)(dev_image->data))[base_index + 1];
          (*color_ptr)[2] = ((uint*)(dev_image->data))[base_index + 0];
          (*color_ptr)[3] = ((uint*)(dev_image->data))[base_index + 3];
        }
      else
        {
          while (i--)
            {
              (*color_ptr)[i] = ((uint*)(dev_image->data))[base_index + i];
            }
        }
    }
}

/* Implementation for read_image with any image data type and int coordinates
   __IMGTYPE__ = image type (image2d_t, ...)
   __RETVAL__  = return value (int4 or uint4 float4)
   __POSTFIX__ = function name postfix (i, ui, f)
   __COORD__   = coordinate type (int, int2, int4)
*/
#define IMPLEMENT_READ_IMAGE_INT_COORD(__IMGTYPE__,__RETVAL__,__POSTFIX__,\
                                            __COORD__)                  \
  __RETVAL__ _CL_OVERLOADABLE read_image##__POSTFIX__ (__IMGTYPE__ image, \
                                                       sampler_t sampler, \
                                                       __COORD__ coord) \
  {                                                                     \
    __RETVAL__ color;                                                   \
    int4 coord4;                                                        \
    INITCOORD##__COORD__(coord4, coord);                                \
    if (__pocl_is_out_of_bounds (*(ADDRESS_SPACE dev_image_t**)&image, coord4, (dev_sampler_t*)&sampler, &color)) \
      {                                                                 \
        return color;                                                   \
      }                                                                 \
    __pocl_read_pixel (&color, (*(ADDRESS_SPACE dev_image_t**)&image), coord4); \
                                                                        \
    return color;                                                       \
  }                                                                     \

/* read_image function instantions */
IMPLEMENT_READ_IMAGE_INT_COORD(image2d_t, uint4, ui, int2)
IMPLEMENT_READ_IMAGE_INT_COORD(image2d_t, int4, i, int2)
IMPLEMENT_READ_IMAGE_INT_COORD(image3d_t, uint4, ui, int4)
IMPLEMENT_READ_IMAGE_INT_COORD(image2d_t, float4, f, int2)

/* OpenCL built-in library: read_image()

   Copyright (c) 2013 Ville Korhonen
   Copyright (c) 2014 Felix Bytow

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
*/

#include "templates.h"
#include "pocl_image_rw_utils.h"

#if (__clang_major__ == 3) && (__clang_minor__ >= 5)
// Clang 3.5 crashes in case trying to cast to the private pointer,
// adding the global qualifier fixes it. Clang 3.4 crashes if it's
// there. The issue is in SROA.
#define ADDRESS_SPACE global
#else
#define ADDRESS_SPACE
#endif

/* checks if integer coord is out of bounds. If out of bounds: Sets coord in
   bounds and returns false OR populates color with border colour and returns
   true. If in bounds, returns false */
int __pocl_is_out_of_bounds (ADDRESS_SPACE dev_image_t* dev_image, int4 coord,
                             dev_sampler_t* dev_sampler, void *color_)
{
  uint4 *color = (uint4*)color_;
  if(*dev_sampler & CLK_ADDRESS_CLAMP_TO_EDGE)
    {
      if (coord.x >= dev_image->width)
        coord.x = dev_image->width-1;
      if (dev_image->height != 0 && coord.y >= dev_image->height)
        coord.y = dev_image->height-1;
      if (dev_image->depth != 0 && coord.z >= dev_image->depth)
        coord.z = dev_image->depth-1;

      if (coord.x < 0)
        coord.x = 0;
      if (coord.y < 0)
        coord.y = 0;
      if (coord.z < 0)
        coord.z = 0;

      return 0;
    }
  if (*dev_sampler & CLK_ADDRESS_CLAMP)
    {
      if(coord.x >= dev_image->width || coord.x < 0 ||
         coord.y >= dev_image->height || coord.y < 0 ||
         (dev_image->depth != 0 && (coord.z >= dev_image->depth || coord.z <0)))
        {
          (*color)[0] = 0;
          (*color)[1] = 0;
          (*color)[2] = 0;

          if (dev_image->order == CL_A || dev_image->order == CL_INTENSITY ||
              dev_image->order == CL_RA || dev_image->order == CL_ARGB ||
              dev_image->order == CL_BGRA || dev_image->order == CL_RGBA)
            (*color)[3] = 0;
          else
            (*color)[3] = 1;

          return 1;
        }
    }
  return 0;
}

/* Reads a four element pixel from image pointed by integer coords. */
void __pocl_read_pixel (void* color, ADDRESS_SPACE dev_image_t* dev_image, int4 coord)
{

  uint4* color_ptr = (uint4*)color;
  int width = dev_image->width;
  int height = dev_image->height;
  int num_channels = dev_image->num_channels;
  int i = num_channels;
  int elem_size = dev_image->elem_size;
  int const base_index =
    (coord.x + coord.y*width + coord.z*height*width) * num_channels;

  if (dev_image->order == CL_A)
    {
      /* these can be garbage
      (*color_ptr)[0] = 0;
      (*color_ptr)[1] = 0;
      (*color_ptr)[2] = 0;
      */
      if (elem_size == 1)
        (*color_ptr)[3] = ((uchar*)(dev_image->data))[base_index];
      else if (elem_size == 2)
        (*color_ptr)[3] = ((ushort*)(dev_image->data))[base_index];
      else if (elem_size == 4)
        (*color_ptr)[3] = ((uint*)(dev_image->data))[base_index];
      return;
    }

  if (elem_size == 1)
    {
      while (i--)
        {
          (*color_ptr)[i] = ((uchar*)(dev_image->data))[base_index + i];
        }
    }
  else if (elem_size == 2)
    {
      while (i--)
        {
          (*color_ptr)[i] = ((ushort*)(dev_image->data))[base_index + i];
        }
    }
  else if (elem_size == 4)
    {
      while (i--)
        {
          (*color_ptr)[i] = ((uint*)(dev_image->data))[base_index + i];
        }
    }
}

/* Implementation for read_image with any image data type and int coordinates
   __IMGTYPE__ = image type (image2d_t, ...)
   __RETVAL__  = return value (int4 or uint4 float4)
   __POSTFIX__ = function name postfix (i, ui, f)
   __COORD__   = coordinate type (int, int2, int4)
*/
#define IMPLEMENT_READ_IMAGE_INT_COORD(__IMGTYPE__,__RETVAL__,__POSTFIX__,\
                                            __COORD__)                  \
  __RETVAL__ _CL_OVERLOADABLE read_image##__POSTFIX__ (__IMGTYPE__ image, \
                                                       sampler_t sampler, \
                                                       __COORD__ coord) \
  {                                                                     \
    __RETVAL__ color;                                                   \
    int4 coord4;                                                        \
    INITCOORD##__COORD__(coord4, coord);                                \
    if (__pocl_is_out_of_bounds (*(ADDRESS_SPACE dev_image_t**)&image, coord4, (dev_sampler_t*)&sampler, &color)) \
      {                                                                 \
        return color;                                                   \
      }                                                                 \
    __pocl_read_pixel (&color, (*(ADDRESS_SPACE dev_image_t**)&image), coord4); \
                                                                        \
    return color;                                                       \
  }                                                                     \

/* read_image function instantions */
IMPLEMENT_READ_IMAGE_INT_COORD(image2d_t, uint4, ui, int2)
IMPLEMENT_READ_IMAGE_INT_COORD(image2d_t, int4, i, int2)
IMPLEMENT_READ_IMAGE_INT_COORD(image3d_t, uint4, ui, int4)
IMPLEMENT_READ_IMAGE_INT_COORD(image2d_t, float4, f, int2)


move blur in separate file

move blur in separate file

float blurred_value(int2 coord, __read_only image2d_t input_data)
{
    const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

    int blur_size = 3;
    float sum = 0.0f;

    for (int a = -blur_size; a < blur_size; a++)
    {
        for (int b = -blur_size; b < blur_size; b++)
        {
            int2 coords = (int2)(coord.x + a, coord.y + b);
            sum += read_imagef(input_data, sampler, coords).x;
        }
    }

    float out = sum / 49.0f;

    return out;
}

__kernel void blu(__read_only image2d_t input_map, __write_only image2d_t output_map)
{
    const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
    int x = get_global_id(0);
	int y = get_global_id(1);

	int2 coord = (int2) (x, y);

    float b = blurred_value(coord, input_map);
    float4 outvalue;
    outvalue.x = b;
    outvalue.y = b;
    outvalue.z = b;
    outvalue.w = 1.0f;

    write_imagef(output_map, coord, outvalue);
}

error: pathspec 'blur.cl' did not match any file(s) known to git


nights (add more to the kernel) Mon May 15 00:16:12 PDT 2017

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/ScrambledAuroras/nn-playground.git/'


Don't choke on doubles in test_rotate

Don't choke on doubles in test_rotate

// TESTING: <<
// TESTING: >>
// TESTING: rotate

#define IMPLEMENT_BODY_G(NAME, BODY, SIZE, GTYPE, SGTYPE, UGTYPE, SUGTYPE)  \
  void NAME##_##GTYPE()                                                     \
  {                                                                         \
    typedef GTYPE gtype;                                                    \
    typedef SGTYPE sgtype;                                                  \
    typedef UGTYPE ugtype;                                                  \
    typedef SUGTYPE sugtype;                                                \
    char const *const typename = #GTYPE;                                    \
    const int vecsize = SIZE;                                               \
    BODY;                                                                   \
  }
#define DEFINE_BODY_G(NAME, EXPR)                                           \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, char    , char  , uchar   , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, char2   , char  , uchar2  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, char3   , char  , uchar3  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, char4   , char  , uchar4  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, char8   , char  , uchar8  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, char16  , char  , uchar16 , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, uchar   , uchar , uchar   , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, uchar2  , uchar , uchar2  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, uchar3  , uchar , uchar3  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, uchar4  , uchar , uchar4  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, uchar8  , uchar , uchar8  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, uchar16 , uchar , uchar16 , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, short   , short , ushort  , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, short2  , short , ushort2 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, short3  , short , ushort3 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, short4  , short , ushort4 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, short8  , short , ushort8 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, short16 , short , ushort16, ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, ushort  , ushort, ushort  , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, ushort2 , ushort, ushort2 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, ushort3 , ushort, ushort3 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, ushort4 , ushort, ushort4 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, ushort8 , ushort, ushort8 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, ushort16, ushort, ushort16, ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, int     , int   , uint    , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, int2    , int   , uint2   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, int3    , int   , uint3   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, int4    , int   , uint4   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, int8    , int   , uint8   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, int16   , int   , uint16  , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, uint    , uint  , uint    , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, uint2   , uint  , uint2   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, uint3   , uint  , uint3   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, uint4   , uint  , uint4   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, uint8   , uint  , uint8   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, uint16  , uint  , uint16  , uint  )      \
  __IF_INT64(                                                               \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, long    , long  , ulong   , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, long2   , long  , ulong2  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, long3   , long  , ulong3  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, long4   , long  , ulong4  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, long8   , long  , ulong8  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, long16  , long  , ulong16 , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, ulong   , ulong , ulong   , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, ulong2  , ulong , ulong2  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, ulong3  , ulong , ulong3  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, ulong4  , ulong , ulong4  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, ulong8  , ulong , ulong8  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, ulong16 , ulong , ulong16 , ulong ))

#define CALL_FUNC_G(NAME)                       \
  NAME##_char    ();                            \
  NAME##_char2   ();                            \
  NAME##_char3   ();                            \
  NAME##_char4   ();                            \
  NAME##_char8   ();                            \
  NAME##_char16  ();                            \
  NAME##_uchar   ();                            \
  NAME##_uchar2  ();                            \
  NAME##_uchar3  ();                            \
  NAME##_uchar4  ();                            \
  NAME##_uchar8  ();                            \
  NAME##_uchar16 ();                            \
  NAME##_short   ();                            \
  NAME##_short2  ();                            \
  NAME##_short3  ();                            \
  NAME##_short4  ();                            \
  NAME##_short8  ();                            \
  NAME##_short16 ();                            \
  NAME##_ushort  ();                            \
  NAME##_ushort2 ();                            \
  NAME##_ushort3 ();                            \
  NAME##_ushort4 ();                            \
  NAME##_ushort8 ();                            \
  NAME##_ushort16();                            \
  NAME##_int     ();                            \
  NAME##_int2    ();                            \
  NAME##_int3    ();                            \
  NAME##_int4    ();                            \
  NAME##_int8    ();                            \
  NAME##_int16   ();                            \
  NAME##_uint    ();                            \
  NAME##_uint2   ();                            \
  NAME##_uint3   ();                            \
  NAME##_uint4   ();                            \
  NAME##_uint8   ();                            \
  NAME##_uint16  ();                            \
  __IF_INT64(                                   \
  NAME##_long    ();                            \
  NAME##_long2   ();                            \
  NAME##_long3   ();                            \
  NAME##_long4   ();                            \
  NAME##_long8   ();                            \
  NAME##_long16  ();                            \
  NAME##_ulong   ();                            \
  NAME##_ulong2  ();                            \
  NAME##_ulong3  ();                            \
  NAME##_ulong4  ();                            \
  NAME##_ulong8  ();                            \
  NAME##_ulong16 ();)

#if __has_extension(c_generic_selections) && defined cl_khr_fp64
# define is_floating(T) _Generic((T)0, float: 1, double: 1, default: 0)
#else
# define is_floating(T) ((T)0.1f > (T)0.0f)
#endif
#define is_signed(T)   ((T)-1 < (T)+1)
#define count_bits(T)  (CHAR_BIT * sizeof(T))

DEFINE_BODY_G
(test_rotate,
 ({
   _CL_STATIC_ASSERT(sgtype, !is_floating(sgtype));
   int patterns[] = {0x01, 0x80, 0x77, 0xee};
   for (int p=0; p<4; ++p) {
     int const bits = count_bits(sgtype);
     int array[64];
     for (int b=0; b<bits; ++b) {
       array[b] = !!(patterns[p] & (1 << (b & 7)));
     }
     typedef union {
       gtype  v;
       sgtype s[16];
     } Tvec;
     for (int shiftbase=0; shiftbase<=bits; ++shiftbase) {
       for (int shiftoffset=0; shiftoffset<(vecsize==1?1:4); ++shiftoffset) {
         Tvec shift;
         Tvec val;
         Tvec shl, shr, rot;
         for (int n=0; n<vecsize; ++n) {
           shift.s[n] = shiftbase + n*shiftoffset;
           // Scalar shift operations undergo integer promotion, i.e.
           // the arguments are converted to int if they are smaller
           // than int. Therefore, overflowing shift counts are
           // interpreted differently.
           if ((vecsize==1) & (sizeof(sgtype) < sizeof(int))) {
             shift.s[n] &= bits-1;
           }
           val.s[n] = 0;
           shl.s[n] = 0;
           shr.s[n] = 0;
           rot.s[n] = 0;
           for (int b=bits; b>=0; --b) {
             int bl = b - (shift.s[n] & (bits-1));
             int br = b + (shift.s[n] & (bits-1));
             int sign = is_signed(sgtype) ? array[bits-1] : 0;
             val.s[n] = (val.s[n] << 1) | array[b];
             shl.s[n] = (shl.s[n] << 1) | (bl < 0 ? 0 : array[bl]);
             shr.s[n] = (shr.s[n] << 1) | (br >= bits ? sign : array[br]);
             rot.s[n] = (rot.s[n] << 1) | array[bl & (bits-1)];
           }
         }
         Tvec res;
         bool equal;
         /* shift left */
         res.v = val.v << shift.v;
         equal = true;
         for (int n=0; n<vecsize; ++n) {
           equal = equal && res.s[n] == shl.s[n];
         }
         if (!equal) {
           for (int n=0; n<vecsize; ++n) {
             printf("FAIL: shift left (<<) type=%s pattern=0x%x shiftbase=%d shiftoffset=%d val=0x%08x count=0x%08x res=0x%08x good=0x%08x\n",
                    typename, patterns[p], shiftbase, shiftoffset,
                    (uint)val.s[n], (uint)shift.s[n],
                    (uint)res.s[n], (uint)shl.s[n]);
           }
           return;
         }
         /* shift right */
         res.v = val.v >> shift.v;
         equal = true;
         for (int n=0; n<vecsize; ++n) {
           equal = equal && res.s[n] == shr.s[n];
         }
         if (!equal) {
           for (int n=0; n<vecsize; ++n) {
             printf("FAIL: shift right (>>) type=%s pattern=0x%x shiftbase=%d shiftoffset=%d val=0x%08x count=0x%08x res=0x%08x good=0x%08x\n",
                    typename, patterns[p], shiftbase, shiftoffset,
                    (uint)val.s[n], (uint)shift.s[n],
                    (uint)res.s[n], (uint)shr.s[n]);
           }
           return;
         }
         /* rotate */
         res.v = rotate(val.v, shift.v);
         equal = true;
         for (int n=0; n<vecsize; ++n) {
           equal = equal && res.s[n] == rot.s[n];
         }
         if (!equal) {
           for (int n=0; n<vecsize; ++n) {
             printf("FAIL: rotate type=%s pattern=0x%x shiftbase=%d shiftoffset=%d val=0x%08x count=0x%08x res=0x%08x good=0x%08x\n",
                    typename, patterns[p], shiftbase, shiftoffset,
                    (uint)val.s[n], (uint)shift.s[n],
                    (uint)res.s[n], (uint)rot.s[n]);
           }
           return;
         }
       }
     }
   }
 })
 )

kernel void test_rotate()
{
  CALL_FUNC_G(test_rotate)
}

// TESTING: <<
// TESTING: >>
// TESTING: rotate

#define IMPLEMENT_BODY_G(NAME, BODY, SIZE, GTYPE, SGTYPE, UGTYPE, SUGTYPE)  \
  void NAME##_##GTYPE()                                                     \
  {                                                                         \
    typedef GTYPE gtype;                                                    \
    typedef SGTYPE sgtype;                                                  \
    typedef UGTYPE ugtype;                                                  \
    typedef SUGTYPE sugtype;                                                \
    char const *const typename = #GTYPE;                                    \
    const int vecsize = SIZE;                                               \
    BODY;                                                                   \
  }
#define DEFINE_BODY_G(NAME, EXPR)                                           \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, char    , char  , uchar   , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, char2   , char  , uchar2  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, char3   , char  , uchar3  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, char4   , char  , uchar4  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, char8   , char  , uchar8  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, char16  , char  , uchar16 , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, uchar   , uchar , uchar   , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, uchar2  , uchar , uchar2  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, uchar3  , uchar , uchar3  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, uchar4  , uchar , uchar4  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, uchar8  , uchar , uchar8  , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, uchar16 , uchar , uchar16 , uchar )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, short   , short , ushort  , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, short2  , short , ushort2 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, short3  , short , ushort3 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, short4  , short , ushort4 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, short8  , short , ushort8 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, short16 , short , ushort16, ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, ushort  , ushort, ushort  , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, ushort2 , ushort, ushort2 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, ushort3 , ushort, ushort3 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, ushort4 , ushort, ushort4 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, ushort8 , ushort, ushort8 , ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, ushort16, ushort, ushort16, ushort)      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, int     , int   , uint    , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, int2    , int   , uint2   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, int3    , int   , uint3   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, int4    , int   , uint4   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, int8    , int   , uint8   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, int16   , int   , uint16  , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, uint    , uint  , uint    , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, uint2   , uint  , uint2   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, uint3   , uint  , uint3   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, uint4   , uint  , uint4   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, uint8   , uint  , uint8   , uint  )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, uint16  , uint  , uint16  , uint  )      \
  __IF_INT64(                                                               \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, long    , long  , ulong   , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, long2   , long  , ulong2  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, long3   , long  , ulong3  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, long4   , long  , ulong4  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, long8   , long  , ulong8  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, long16  , long  , ulong16 , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  1, ulong   , ulong , ulong   , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  2, ulong2  , ulong , ulong2  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  3, ulong3  , ulong , ulong3  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  4, ulong4  , ulong , ulong4  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR,  8, ulong8  , ulong , ulong8  , ulong )      \
  IMPLEMENT_BODY_G(NAME, EXPR, 16, ulong16 , ulong , ulong16 , ulong ))

#define CALL_FUNC_G(NAME)                       \
  NAME##_char    ();                            \
  NAME##_char2   ();                            \
  NAME##_char3   ();                            \
  NAME##_char4   ();                            \
  NAME##_char8   ();                            \
  NAME##_char16  ();                            \
  NAME##_uchar   ();                            \
  NAME##_uchar2  ();                            \
  NAME##_uchar3  ();                            \
  NAME##_uchar4  ();                            \
  NAME##_uchar8  ();                            \
  NAME##_uchar16 ();                            \
  NAME##_short   ();                            \
  NAME##_short2  ();                            \
  NAME##_short3  ();                            \
  NAME##_short4  ();                            \
  NAME##_short8  ();                            \
  NAME##_short16 ();                            \
  NAME##_ushort  ();                            \
  NAME##_ushort2 ();                            \
  NAME##_ushort3 ();                            \
  NAME##_ushort4 ();                            \
  NAME##_ushort8 ();                            \
  NAME##_ushort16();                            \
  NAME##_int     ();                            \
  NAME##_int2    ();                            \
  NAME##_int3    ();                            \
  NAME##_int4    ();                            \
  NAME##_int8    ();                            \
  NAME##_int16   ();                            \
  NAME##_uint    ();                            \
  NAME##_uint2   ();                            \
  NAME##_uint3   ();                            \
  NAME##_uint4   ();                            \
  NAME##_uint8   ();                            \
  NAME##_uint16  ();                            \
  __IF_INT64(                                   \
  NAME##_long    ();                            \
  NAME##_long2   ();                            \
  NAME##_long3   ();                            \
  NAME##_long4   ();                            \
  NAME##_long8   ();                            \
  NAME##_long16  ();                            \
  NAME##_ulong   ();                            \
  NAME##_ulong2  ();                            \
  NAME##_ulong3  ();                            \
  NAME##_ulong4  ();                            \
  NAME##_ulong8  ();                            \
  NAME##_ulong16 ();)

#if __has_extension(c_generic_selections)
# define is_floating(T) _Generic((T)0, float: 1, double: 1, default: 0)
#else
# define is_floating(T) ((T)0.1f > (T)0.0f)
#endif
#define is_signed(T)   ((T)-1 < (T)+1)
#define count_bits(T)  (CHAR_BIT * sizeof(T))

DEFINE_BODY_G
(test_rotate,
 ({
   _CL_STATIC_ASSERT(sgtype, !is_floating(sgtype));
   int patterns[] = {0x01, 0x80, 0x77, 0xee};
   for (int p=0; p<4; ++p) {
     int const bits = count_bits(sgtype);
     int array[64];
     for (int b=0; b<bits; ++b) {
       array[b] = !!(patterns[p] & (1 << (b & 7)));
     }
     typedef union {
       gtype  v;
       sgtype s[16];
     } Tvec;
     for (int shiftbase=0; shiftbase<=bits; ++shiftbase) {
       for (int shiftoffset=0; shiftoffset<(vecsize==1?1:4); ++shiftoffset) {
         Tvec shift;
         Tvec val;
         Tvec shl, shr, rot;
         for (int n=0; n<vecsize; ++n) {
           shift.s[n] = shiftbase + n*shiftoffset;
           // Scalar shift operations undergo integer promotion, i.e.
           // the arguments are converted to int if they are smaller
           // than int. Therefore, overflowing shift counts are
           // interpreted differently.
           if ((vecsize==1) & (sizeof(sgtype) < sizeof(int))) {
             shift.s[n] &= bits-1;
           }
           val.s[n] = 0;
           shl.s[n] = 0;
           shr.s[n] = 0;
           rot.s[n] = 0;
           for (int b=bits; b>=0; --b) {
             int bl = b - (shift.s[n] & (bits-1));
             int br = b + (shift.s[n] & (bits-1));
             int sign = is_signed(sgtype) ? array[bits-1] : 0;
             val.s[n] = (val.s[n] << 1) | array[b];
             shl.s[n] = (shl.s[n] << 1) | (bl < 0 ? 0 : array[bl]);
             shr.s[n] = (shr.s[n] << 1) | (br >= bits ? sign : array[br]);
             rot.s[n] = (rot.s[n] << 1) | array[bl & (bits-1)];
           }
         }
         Tvec res;
         bool equal;
         /* shift left */
         res.v = val.v << shift.v;
         equal = true;
         for (int n=0; n<vecsize; ++n) {
           equal = equal && res.s[n] == shl.s[n];
         }
         if (!equal) {
           for (int n=0; n<vecsize; ++n) {
             printf("FAIL: shift left (<<) type=%s pattern=0x%x shiftbase=%d shiftoffset=%d val=0x%08x count=0x%08x res=0x%08x good=0x%08x\n",
                    typename, patterns[p], shiftbase, shiftoffset,
                    (uint)val.s[n], (uint)shift.s[n],
                    (uint)res.s[n], (uint)shl.s[n]);
           }
           return;
         }
         /* shift right */
         res.v = val.v >> shift.v;
         equal = true;
         for (int n=0; n<vecsize; ++n) {
           equal = equal && res.s[n] == shr.s[n];
         }
         if (!equal) {
           for (int n=0; n<vecsize; ++n) {
             printf("FAIL: shift right (>>) type=%s pattern=0x%x shiftbase=%d shiftoffset=%d val=0x%08x count=0x%08x res=0x%08x good=0x%08x\n",
                    typename, patterns[p], shiftbase, shiftoffset,
                    (uint)val.s[n], (uint)shift.s[n],
                    (uint)res.s[n], (uint)shr.s[n]);
           }
           return;
         }
         /* rotate */
         res.v = rotate(val.v, shift.v);
         equal = true;
         for (int n=0; n<vecsize; ++n) {
           equal = equal && res.s[n] == rot.s[n];
         }
         if (!equal) {
           for (int n=0; n<vecsize; ++n) {
             printf("FAIL: rotate type=%s pattern=0x%x shiftbase=%d shiftoffset=%d val=0x%08x count=0x%08x res=0x%08x good=0x%08x\n",
                    typename, patterns[p], shiftbase, shiftoffset,
                    (uint)val.s[n], (uint)shift.s[n],
                    (uint)res.s[n], (uint)rot.s[n]);
           }
           return;
         }
       }
     }
   }
 })
 )

kernel void test_rotate()
{
  CALL_FUNC_G(test_rotate)
}


Updated cl filename. Expecting use of multiple types in future

inline int get_index(int nelems, int index ) {
/* Fetch a linear index to work on given # of elements
 * and previous index. -1 if start
 */

  if (index == -1)  {
    index = get_global_id(0);
  } else {
    index += get_global_size(0);
  }

  if (index >= nelems) index = -1;

  return index;
}

__kernel void add(__global float *out, __global const float *x, __global const float *y, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = x[id] + y[id];
    id = get_index(N, id);
  }
}

__kernel void minus(__global float *out, __global const float *x, __global const float *y, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = x[id] - y[id];
    id = get_index(N, id);
  }
}

__kernel void divide(__global float *out, __global const float *x, __global const float *y, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = x[id] /  y[id];
    id = get_index(N, id);
  }
}

__kernel void times(__global float *out, __global const float *x, __global const float *y, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = x[id] *  y[id];
    id = get_index(N, id);
  }
}

__kernel void scalar_times(__global float *out, float w, global const float *x, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = w * x[id];
    id = get_index(N, id);
  }
}

__kernel void scalar_add(__global float *out, float w, global const float *x, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = w + x[id];
    id = get_index(N, id);
  }
}

__kernel void scalar_minus(__global float *out, float w, global const float *x, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = w - x[id];
    id = get_index(N, id);
  }
}

__kernel void minus_scalar(__global float *out, global const float *x, float w, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = x[id] - w;
    id = get_index(N, id);
  }
}

__kernel void scalar_divide(__global float *out, float w, global const float *x, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = w / x[id];
    id = get_index(N, id);
  }
}

__kernel void divide_scalar(__global float *out, global const float *x, float w, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = x[id] / w;
    id = get_index(N, id);
  }
}

__kernel void exponential(__global float *out, __global const float *x, int N) {
  int id = get_index(N, -1);
  while(id >= 0) {
    out[id] = exp(x[id]);
    id = get_index(N, id);
  }
}

error: pathspec 'cl/matlab_kernels_float.cl' did not match any file(s) known to git


Serialize malloc

Serialize malloc

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/CLBplustree/CLBplustree.git/'


Renamed some variables

Renamed some variables

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/HongjianLi/idock.git/'


Updated cl filename. Expecting use of multiple types in future

git-svn-id: 807bbae5e1e1e6af950d5bf3f1690f093529d808@68 319ae97b-01e6-675e-e8e9-a1b9ca5b6390


Kernel to update the sate of LIF layers that have adaptive Wmax and VthRest parameters.

git-svn-id: 602a26e73ae9f2a14e2bb95676f2e137fcc39919@3566 7c0444ca-5550-0410-9008-faecdbe78eb5

#include "LIF_params.h"
#include "cl_random.hcl"
#include "../utils/pv_random.h"

#ifndef PV_USE_OPENCL
#  include <math.h>
#  define EXP expf
#  define CL_KERNEL
#  define CL_MEM_GLOBAL
#  define CL_MEM_LOCAL
#else  /* compiling with OpenCL */
#  define EXP exp
#  define CL_KERNEL       __kernel
#  define CL_MEM_GLOBAL   __global
#  define CL_MEM_LOCAL    __local
#  include "conversions.hcl"
#endif

//
// update the state of a retinal layer (spiking)
//
//    assume called with 1D kernel
//
CL_KERNEL
void LIF_update_state_localWmaxVth(
    const float time,
    const float dt,

    const int nx,
    const int ny,
    const int nf,
    const int nb,

    const float tauWmax,
    const float tauVthRest,
    const float alphaVthRest,
    const float alphaW,
    const float averageR,

    CL_MEM_GLOBAL LIF_params * params,
    CL_MEM_GLOBAL uint4 * rnd,
    CL_MEM_GLOBAL float * V,
    CL_MEM_GLOBAL float * Vth,
    CL_MEM_GLOBAL float * G_E,
    CL_MEM_GLOBAL float * G_I,
    CL_MEM_GLOBAL float * G_IB,
    CL_MEM_GLOBAL float * phiExc,
    CL_MEM_GLOBAL float * phiInh,
    CL_MEM_GLOBAL float * phiInhB,
    CL_MEM_GLOBAL float * R,
    CL_MEM_GLOBAL float * Wmax,
    CL_MEM_GLOBAL float * VthRest,
    CL_MEM_GLOBAL float * activity)
{
   int k;

   const float exp_tauE    = EXP(-dt/params->tauE);
   const float exp_tauI    = EXP(-dt/params->tauI);
   const float exp_tauIB   = EXP(-dt/params->tauIB);
   const float exp_tauVth  = EXP(-dt/params->tauVth);

   const float dt_sec = .001 * dt;   // convert to seconds
   const float tauRate = params->tauRate;
   const float exp_tauRate = EXP(-dt/tauRate);
   const float exp_tauWmax = EXP(-dt/tauWmax);
   const float exp_tauVthRest = EXP(-dt/tauVthRest);

#ifndef PV_USE_OPENCL

for (k = 0; k < nx*ny*nf; k++) {
#else
   k = get_global_id(0);
#endif

   int kex = kIndexExtended(k, nx, ny, nf, nb);

   //
   // kernel (nonheader part) begins here
   //

   // local param variables
   float tau, Vrest, Vexc, Vinh, VinhB, deltaVth, Vthrest;

   const float GMAX = 10.0;

   // local variables
   float l_activ;

   uint4 l_rnd = rnd[k];

   float l_V   = V[k];
   float l_Vth = Vth[k];
   float l_VthRest = VthRest[k];

   float l_G_E  = G_E[k];
   float l_G_I  = G_I[k];
   float l_G_IB = G_IB[k];

   float l_phiExc  = phiExc[k];
   float l_phiInh  = phiInh[k];
   float l_phiInhB = phiInhB[k];

   float l_R = R[k];
   float l_Wmax = Wmax[kex];

   // temporary arrays
   float tauInf, VmemInf;

   //
   // start of LIF2_update_exact_linear
   //

   // define local param variables
   //
   tau   = params->tau;
   Vexc  = params->Vexc;
   Vinh  = params->Vinh;
   VinhB = params->VinhB;
   Vrest = params->Vrest;

   Vthrest  = params->VthRest;
   deltaVth = params->deltaVth;

   // add noise
   //

#undef CLRANDOM

#ifdef CLRANDOM
   l_rnd = cl_random_get(l_rnd);
   if (cl_random_prob(l_rnd) < dt_sec*params->noiseFreqE) {
      l_rnd = cl_random_get(l_rnd);
      l_phiExc = l_phiExc + params->noiseAmpE*cl_random_prob(l_rnd);
   }

   l_rnd = cl_random_get(l_rnd);
   if (cl_random_prob(l_rnd) < dt_sec*params->noiseFreqI) {
      l_rnd = cl_random_get(l_rnd);
      l_phiInh = l_phiInh + params->noiseAmpI*cl_random_prob(l_rnd);
   }

   l_rnd = cl_random_get(l_rnd);
   if (cl_random_prob(l_rnd) < dt_sec*params->noiseFreqIB) {
      l_rnd = cl_random_get(l_rnd);
      l_phiInhB = l_phiInhB + params->noiseAmpIB*cl_random_prob(l_rnd);
   }
#else
   if (pv_random_prob() < dt_sec*params->noiseFreqE) {
      l_phiExc = l_phiExc + params->noiseAmpE*pv_random_prob();
   }

   if (pv_random_prob() < dt_sec*params->noiseFreqI) {
      l_phiInh = l_phiInh + params->noiseAmpI*pv_random_prob();
   }

   if (pv_random_prob() < dt_sec*params->noiseFreqIB) {
      l_phiInhB = l_phiInhB + params->noiseAmpIB*pv_random_prob();
   }
#endif
   // l_phi channels must be divided by tau_E, ... in each channel expressed in ms?!
   l_G_E  = l_phiExc  + l_G_E *exp_tauE;
   l_G_I  = l_phiInh  + l_G_I *exp_tauI;
   l_G_IB = l_phiInhB + l_G_IB*exp_tauIB;

   l_G_E  = (l_G_E  > GMAX) ? GMAX : l_G_E;
   l_G_I  = (l_G_I  > GMAX) ? GMAX : l_G_I;
   l_G_IB = (l_G_IB > GMAX) ? GMAX : l_G_IB;

   tauInf  = (dt/tau) * (1.0 + l_G_E + l_G_I + l_G_IB);
   VmemInf = (Vrest + l_G_E*Vexc + l_G_I*Vinh + l_G_IB*VinhB)
           / (1.0 + l_G_E + l_G_I + l_G_IB);

   l_V = VmemInf + (l_V - VmemInf)*EXP(-tauInf);;

   //
   // start of LIF2_update_finish
   //
   l_Vth = l_VthRest + (l_Vth - l_VthRest)*exp_tauVth;

   //
   // start of update_f
   //

   bool fired_flag = (l_V > l_Vth);

   l_activ = fired_flag ? 1.0f             : 0.0f;

   // update average rate
   l_R = (l_activ / tauRate) + l_R*exp_tauRate;
   // multiply by 1000.0 to get the firing rate per second, which is how averageR is estimated
   // dynamics I: dynamics linked to average firing rate
   l_Wmax = - alphaW * (1000.0*l_R - averageR) + (l_Wmax + alphaW * (1000.0*l_R - averageR)) * exp_tauWmax;
   // dynamics II: linked to firing or not firing information - like VthRest does
   //l_Wmax = fired_flag ? (l_Wmax - 0.0001) : (l_Wmax + 0.00000001);

   // dynamics I: linked to average firing rate
   //l_VthRest =  alphaVthRest * (1000.0*l_R - averageR) + (l_VthRest - alphaVthRest * (1000.0*l_R - averageR)) * exp_tauVthRest;
   // dynamics II: linked to firing or not firing information
   l_VthRest = fired_flag ? (l_VthRest + 1000.0/tauVthRest ) : l_VthRest;
   l_VthRest = Vthrest + (l_VthRest - Vthrest) * exp_tauVthRest;

   l_V     = fired_flag ? Vrest            : l_V;
   l_Vth   = fired_flag ? l_Vth + deltaVth : l_Vth;
   l_G_IB  = fired_flag ? l_G_IB + 1.0f    : l_G_IB;

   //
   // These actions must be done outside of kernel
   //    1. set activity to 0 in boundary (if needed)
   //    2. update active indices
   //

   // store local variables back to global memory
   //
   rnd[k] = l_rnd;

   activity[kex] = l_activ;

   R[k] = l_R;
   Wmax[kex] = l_Wmax;
   VthRest[k] = l_VthRest;

   V[k]   = l_V;
   Vth[k] = l_Vth;

   G_E[k]  = l_G_E;
   G_I[k]  = l_G_I;
   G_IB[k] = l_G_IB;

   phiExc[k]  = 0.0f;
   phiInh[k]  = 0.0f;
   phiInhB[k] = 0.0f;

#ifndef PV_USE_OPENCL
   } // loop over k
#endif

}

#include "LIF_params.h"
#include "cl_random.hcl"
#include "../utils/pv_random.h"

#ifndef PV_USE_OPENCL
#  include <math.h>
#  define EXP expf
#  define CL_KERNEL
#  define CL_MEM_GLOBAL
#  define CL_MEM_LOCAL
#else  /* compiling with OpenCL */
#  define EXP exp
#  define CL_KERNEL       __kernel
#  define CL_MEM_GLOBAL   __global
#  define CL_MEM_LOCAL    __local
#  include "conversions.hcl"
#endif

//
// update the state of a retinal layer (spiking)
//
//    assume called with 1D kernel
//
CL_KERNEL
void LIF_update_state_localWmaxVth(
    const float time,
    const float dt,

    const int nx,
    const int ny,
    const int nf,
    const int nb,

    const float tauWmax,
    const float tauVthRest,
    const float alphaVthRest,
    const float alphaW,
    const float averageR,

    CL_MEM_GLOBAL LIF_params * params,
    CL_MEM_GLOBAL uint4 * rnd,
    CL_MEM_GLOBAL float * V,
    CL_MEM_GLOBAL float * Vth,
    CL_MEM_GLOBAL float * G_E,
    CL_MEM_GLOBAL float * G_I,
    CL_MEM_GLOBAL float * G_IB,
    CL_MEM_GLOBAL float * phiExc,
    CL_MEM_GLOBAL float * phiInh,
    CL_MEM_GLOBAL float * phiInhB,
    CL_MEM_GLOBAL float * R,
    CL_MEM_GLOBAL float * Wmax,
    CL_MEM_GLOBAL float * VthRest,
    CL_MEM_GLOBAL float * activity)
{
   int k;

   const float exp_tauE    = EXP(-dt/params->tauE);
   const float exp_tauI    = EXP(-dt/params->tauI);
   const float exp_tauIB   = EXP(-dt/params->tauIB);
   const float exp_tauVth  = EXP(-dt/params->tauVth);

   const float dt_sec = .001 * dt;   // convert to seconds
   const float tauRate = params->tauRate;
   const float exp_tauRate = EXP(-dt/tauRate);
   const float exp_tauWmax = EXP(-dt/tauWmax);
   const float exp_tauVthRest = EXP(-dt/tauVthRest);

#ifndef PV_USE_OPENCL

for (k = 0; k < nx*ny*nf; k++) {
#else
   k = get_global_id(0);
#endif

   int kex = kIndexExtended(k, nx, ny, nf, nb);

   //
   // kernel (nonheader part) begins here
   //

   // local param variables
   float tau, Vrest, Vexc, Vinh, VinhB, deltaVth;

   const float GMAX = 10.0;

   // local variables
   float l_activ;

   uint4 l_rnd = rnd[k];

   float l_V   = V[k];
   float l_Vth = Vth[k];
   float l_VthRest = VthRest[k];

   float l_G_E  = G_E[k];
   float l_G_I  = G_I[k];
   float l_G_IB = G_IB[k];

   float l_phiExc  = phiExc[k];
   float l_phiInh  = phiInh[k];
   float l_phiInhB = phiInhB[k];

   float l_R = R[k];
   float l_Wmax = Wmax[kex];

   // temporary arrays
   float tauInf, VmemInf;

   //
   // start of LIF2_update_exact_linear
   //

   // define local param variables
   //
   tau   = params->tau;
   Vexc  = params->Vexc;
   Vinh  = params->Vinh;
   VinhB = params->VinhB;
   Vrest = params->Vrest;

   //VthRest  = params->VthRest;
   deltaVth = params->deltaVth;

   // add noise
   //

#undef CLRANDOM

#ifdef CLRANDOM
   l_rnd = cl_random_get(l_rnd);
   if (cl_random_prob(l_rnd) < dt_sec*params->noiseFreqE) {
      l_rnd = cl_random_get(l_rnd);
      l_phiExc = l_phiExc + params->noiseAmpE*cl_random_prob(l_rnd);
   }

   l_rnd = cl_random_get(l_rnd);
   if (cl_random_prob(l_rnd) < dt_sec*params->noiseFreqI) {
      l_rnd = cl_random_get(l_rnd);
      l_phiInh = l_phiInh + params->noiseAmpI*cl_random_prob(l_rnd);
   }

   l_rnd = cl_random_get(l_rnd);
   if (cl_random_prob(l_rnd) < dt_sec*params->noiseFreqIB) {
      l_rnd = cl_random_get(l_rnd);
      l_phiInhB = l_phiInhB + params->noiseAmpIB*cl_random_prob(l_rnd);
   }
#else
   if (pv_random_prob() < dt_sec*params->noiseFreqE) {
      l_phiExc = l_phiExc + params->noiseAmpE*pv_random_prob();
   }

   if (pv_random_prob() < dt_sec*params->noiseFreqI) {
      l_phiInh = l_phiInh + params->noiseAmpI*pv_random_prob();
   }

   if (pv_random_prob() < dt_sec*params->noiseFreqIB) {
      l_phiInhB = l_phiInhB + params->noiseAmpIB*pv_random_prob();
   }
#endif
   // l_phi channels must be divided by tau_E, ... in each channel expressed in ms?!
   l_G_E  = l_phiExc  + l_G_E *exp_tauE;
   l_G_I  = l_phiInh  + l_G_I *exp_tauI;
   l_G_IB = l_phiInhB + l_G_IB*exp_tauIB;

   l_G_E  = (l_G_E  > GMAX) ? GMAX : l_G_E;
   l_G_I  = (l_G_I  > GMAX) ? GMAX : l_G_I;
   l_G_IB = (l_G_IB > GMAX) ? GMAX : l_G_IB;

   tauInf  = (dt/tau) * (1.0 + l_G_E + l_G_I + l_G_IB);
   VmemInf = (Vrest + l_G_E*Vexc + l_G_I*Vinh + l_G_IB*VinhB)
           / (1.0 + l_G_E + l_G_I + l_G_IB);

   l_V = VmemInf + (l_V - VmemInf)*EXP(-tauInf);;

   //
   // start of LIF2_update_finish
   //
   // update average rate
   l_R = (l_activ / tauRate) + l_R*exp_tauRate;
   // multiply by 1000.0 to get the firing rate per second, which is how averageR is estimated
   l_Wmax = - alphaW * (1000.0*l_R - averageR) + (l_Wmax + alphaW * (1000.0*l_R - averageR)) * exp_tauWmax;
   l_VthRest = - alphaVthRest * (1000.0*l_R - averageR) + (l_VthRest + alphaVthRest * (1000.0*l_R - averageR)) * exp_tauVthRest;
   l_Vth = l_VthRest + (l_Vth - l_VthRest)*exp_tauVth;

   //
   // start of update_f
   //

//   l_G_E  = (l_G_E  > GMAX) ? GMAX : l_G_E;
//   l_G_I  = (l_G_I  > GMAX) ? GMAX : l_G_I;
//   l_G_IB = (l_G_IB > GMAX) ? GMAX : l_G_IB;

   bool fired_flag = (l_V > l_Vth);

   l_activ = fired_flag ? 1.0f             : 0.0f;
   l_V     = fired_flag ? Vrest            : l_V;
   l_Vth   = fired_flag ? l_Vth + deltaVth : l_Vth;
   l_G_IB  = fired_flag ? l_G_IB + 1.0f    : l_G_IB;

   //
   // These actions must be done outside of kernel
   //    1. set activity to 0 in boundary (if needed)
   //    2. update active indices
   //

   // store local variables back to global memory
   //
   rnd[k] = l_rnd;

   activity[kex] = l_activ;

   R[k] = l_R;
   Wmax[kex] = l_Wmax;
   VthRest[k] = l_VthRest;

   V[k]   = l_V;
   Vth[k] = l_Vth;

   G_E[k]  = l_G_E;
   G_I[k]  = l_G_I;
   G_IB[k] = l_G_IB;

   phiExc[k]  = 0.0f;
   phiInh[k]  = 0.0f;
   phiInhB[k] = 0.0f;

#ifndef PV_USE_OPENCL
   } // loop over k
#endif

}


Commit some broken dreams

Commit some broken dreams

struct Scene {
    int n_objects;
    __global struct ComplexObject * objects;
};

float sd_object(__global struct ComplexObject obj, const float3 p) {
    float dist = 1000.0f;
    for (int k = 0; k < obj->n_u_sphere; ++k) {
        dist = union_sd(sd_sphere(obj->u_sphere[k], p), dist);
    }
    for (int k = 0; k < obj->n_u_plane; ++k) {
        dist = union_sd(sd_plane(obj->u_plane[k], p), dist);
    }
    for (int k = 0; k < obj->n_u_box; ++k) {
        dist = union_sd(sd_box(obj->u_box[k], p), dist);
    }

    for (int k = 0; k < obj->n_int_sphere; ++k) {
        dist = intersect_sd(sd_sphere(obj->int_sphere[k], p), dist);
    }
    for (int k = 0; k < obj->n_int_plane; ++k) {
        dist = intersect_sd(sd_plane(obj->int_plane[k], p), dist);
    }
    for (int k = 0; k < obj->n_int_box; ++k) {
        dist = intersect_sd(sd_box(obj->int_box[k], p), dist);
    }

    float dist_sub = 1000.0f;
    for (int k = 0; k < obj->n_sub_sphere; ++k) {
        dist_sub = union_sd(sd_sphere(obj->sub_sphere[k], p), dist_sub);
    }
    for (int k = 0; k < obj->n_sub_plane; ++k) {
        dist_sub = union_sd(sd_plane(obj->sub_plane[k], p), dist_sub);
    }
    for (int k = 0; k < obj->n_sub_box; ++k) {
        dist_sub = union_sd(sd_box(obj->sub_box[k], p), dist_sub);
    }

    dist = subtract_sd(dist, dist_sub);
    return dist;
}

error: pathspec 'gpgpu/broken_dreams/super_objects.cl' did not match any file(s) known to git


adjust hdr_rgb kernel, to fit for V4L2_PIX_FMT_RGBA32 and CL_RGBA when
 use 'rgba' as the input format.

* fix current kernel only support CL_R as image channel order, for CL_RGBA
G and B channel will always set to 0.


does the highlighting work?

does the highlighting work?

;; Copyright 2008 Kat Marchan

;; This file is part of sykosomatic

;; sykosomatic is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; sykosomatic is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with sykosomatic.  If not, see <http://www.gnu.org/licenses/>.

(in-package #:sykosomatic)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;============================================== Game ==========================================;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
(defun main ()
  (format t "Starting game...")
  (let ((rooms (get-objects-from-directory *rooms-directory*))
	(players (get-objects-from-directory *players-directory*)))
    (eval rooms)
    (eval players)))

error: pathspec 'game.cl' did not match any file(s) known to git


Removed seed

Removed seed


adds smme kernel

adds smme kernel

__kernel void second_moment_matrix_elements(
    __global float* ddx,
    __global float* ddy,
    __global float* xx,
    __global float* xy,
    __global float* yy
)
{
    int i = get_global_id(0);

    float dx = ddx[i];
    float dy = ddy[i];

    xx[i] = dx*dx;
    xy[i] = dx*dy;
    yy[i] = dy*dy;
}

error: pathspec 'kernels/smme.cl' did not match any file(s) known to git


updated kernel

updated kernel

remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/Saytiras/Massively-Parallel-Algorithms.git/'