⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reduce.br

📁 用于GPU通用计算的编程语言BrookGPU 0.4
💻 BR
字号:
// reduce.br// A stress-test for reduction functionality.// The matrix-multiplication operation is associative// but not commutative, so this test confirms// that a runtime correctly groups elements// when performing reductions (and thus respects// noncommutative operations).#include <stdio.h>extern void printf();extern void streamRead();extern void streamWrite();void __printf_cpu_inner (float f) {   printf("%f\n",f);}reduce void foo (float4 a<>, float4 c,		 float d[10][10], reduce float4 e<>) {  e.x = a.x+e.x+d[c.xy]+d[c.yx];  e.y = a.y+e.y+c.y;  e.z = a.z+e.z+c.w;  e.w = a.w+e.w+c.z;}// [a.x a.y] [b.x b.y]   [a.x*b.x+a.y*b.z a.x*b.y+a.y*b.w ]// [a.z a.w] [b.z b.w] = [a.z*b.x+a.w*b.z a.z*b.y+a.w*b.w ]reduce void matrix_mult (reduce float4 a<>, float4 b<>) {   a=float4 (a.x*b.x+a.y*b.z, a.x*b.y+a.y*b.w,              a.z*b.x+a.w*b.z, a.z*b.y+a.w*b.w);}float myrand (unsigned int *seed) {   unsigned int rand_max =1509281;   *seed = (*seed +26129357)%rand_max;   if (*seed<(rand_max/5)*2)      return .5;   else if (*seed<rand_max/2)      return 1;   else if (*seed<(rand_max/4)*3)      return 0;   else if (*seed<(rand_max/11)*10)      return .25;   else      return 4;}int main () {  unsigned int seed=06294306;  float4 matrices<1,100>;  float4 result;  float4 quadresult <1,4>;  float4 qresult[1][4];  float4 a<10, 10>;  float b<10, 10>;  float d<10, 10>;  float e<10, 10>;    float4 data_a[10][10];  float data_b[10][10];  float4 c = float4(1.0f, 0.0f, 3.2f, 5.0f);  float data_d[10][10];  float4 output;  int i,j;  for (i=0; i<10; i++)    for (j=0; j<10; j++) {      data_a[i][j].x = .0625f+((float) i) + ((float) j) / 10.0f;      data_a[i][j].y=data_a[i][j].z=data_a[i][j].w=data_a[i][j].x;      data_b[i][j] = .125+((float) j) + ((float) i) / 10.0f;      data_d[i][j] = .0625+(float)j/8+((float) i) / 64.0f;          }  streamRead(a, data_a);  streamRead(b, data_b);  streamRead(d, data_d);  foo(a,c,d,output);  for (i=0; i<10; i++) {    for (j=0; j<10; j++) {       data_a[i][j].x=myrand(&seed);       data_a[i][j].y=myrand(&seed);       data_a[i][j].z=myrand(&seed);       data_a[i][j].w=myrand(&seed);    }  }  streamRead(matrices,data_a);  matrix_mult(result,matrices);  matrix_mult(quadresult,matrices);    streamWrite(quadresult,qresult);  printf("begin adding float4 test ...\n");  printf("{%3.0f %3.0f %3.0f %3.0f}\n", output.x,output.y,output.z,output.w);  printf("matrix multiply: together is:\n");  printf("[%3.0f %3.0f]\n[%3.0f %3.0f]\n",result.x,result.y,result.z,result.w);  printf("-------------------------------\n");  printf("[%3.0f %3.0f]\n[%3.0f %3.0f]\n\n",         qresult[0][0].x,qresult[0][0].y,qresult[0][0].z,qresult[0][0].w);  printf("[%3.0f %3.0f]\n[%3.0f %3.0f]\n\n",         qresult[0][1].x,qresult[0][1].y,qresult[0][1].z,qresult[0][1].w);  printf("[%3.0f %3.0f]\n[%3.0f %3.0f]\n\n",           qresult[0][2].x,qresult[0][2].y,qresult[0][2].z,qresult[0][2].w);  printf("[%3.0f %3.0f]\n[%3.0f %3.0f]\n",           qresult[0][3].x,qresult[0][3].y,qresult[0][3].z,qresult[0][3].w);  matrix_mult(result,quadresult);  printf ("------------------------------\n");  printf ("Two stage multiply gives:\n");  printf("[%3.0f %3.0f]\n[%3.0f %3.0f]\n",result.x,result.y,result.z,result.w);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -