fft_kernelstring.cpp 67.5 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
                kernel_string += string("ang = 0.0f;\n");  
                break;
            case 1 : 
                kernel_string += string("ang = dir*(" + pi_mult_hex + string(") * (")+expr+");\n");   
                break;
            default:     
                float pi2=0x1.921fb54442d18p+2;
                char tw[200];
                sprintf(tw,"%a",pi2*num / (float) denom);
                kernel_string += string("ang = dir*(" + string(tw) + string(") * (")+expr+");\n"); 
                break;
        }
    }
    kernel_string += varRes+string(" = (float2)(native_cos(ang), native_sin(ang));\n");
}

1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
static void
insertSinCosCalcDirect(string & kernel_string, cl_fft_plan *plan, int num, int denom , string & expr, string & varRes)
{
  if(denom & (denom-1)) {
    kernel_string += string("ang = dir*(2.0f*M_PI*") + num2str(num) + string("/") + num2str(denom) + string(")*("+expr+");\n");
  } else {
    // denom is a power of two                                                                                                                                                                                          
    int logDenom =0;
    int d= denom;
    while (d != 1) {
      logDenom++;
      d >>= 1;
    }
    int pi_exp=1+1-logDenom;
    string pi_mult_hex = string("0x1.921fb54442d18p") + (pi_exp>0 ? string("+") : string("")) + num2str(pi_exp);
    switch (num) {
    case 0 :
      kernel_string += string("ang = 0.0f;\n");
      break;
    case 1 :
      kernel_string += string("ang = dir*(" + pi_mult_hex + string(") * (")+expr+");\n");
      break;
    default:
      float pi2=0x1.921fb54442d18p+2;
      char tw[200];
      sprintf(tw,"%a",pi2*num / (float) denom);
      kernel_string += string("ang = dir*(" + string(tw) + string(") * (")+expr+");\n");
      break;
    }
  }
   
  kernel_string += string("{\n");
  kernel_string += string(" float _tmpcos,_tmpsin;\n");
  kernel_string += string(" _tmpsin=sincos(ang,&_tmpcos);\n");
  kernel_string += varRes+string(" = (float2)(_tmpcos, _tmpsin);\n");
  kernel_string += string("}\n");
 
}


1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
static void 
insertSinCosCalc(string & kernel_string, cl_fft_plan *plan, int num, int denom , string & expr, string & varRes) 
{
    if(denom & (denom-1)) {
        kernel_string += string("ang = dir*(2.0f*M_PI*") + num2str(num) + string("/") + num2str(denom) + string(")*("+expr+");\n");        
        kernel_string += varRes+string(" = (float2)(native_cos(ang), native_sin(ang));\n");
    } else {

        switch (num) { 
            case 0 : 
                kernel_string += string("ang = 0.0f;\n");  
                kernel_string += varRes+string(" = (float2)(1.0f, 0.0f);\n");
                break;
            default:     
                int num_norm = num*(plan->N1 * plan->N2)/denom;
                
                if(num_norm % plan->N1 == 0) {
                    
                    kernel_string += string("{ int ang_index = ") + num2str(num_norm) + string(" * ( ") + expr + string(") ; \n");  
                    kernel_string += string("  int ang_index_k = (ang_index >> " ) + num2str(plan->logN1)+ string(") & "+num2str(plan->N2-1)+ ";\n");
1077
                    kernel_string += string("  cos_sinLUT1("+varRes+",dir,ang_index_k,cossinLUT2);\n");
1078
1079
1080
1081
1082
                    kernel_string += string("}\n");
                } else {
                    kernel_string += string("{ int ang_index = ") + num2str(num_norm) + string(" * ( ") + expr + string(") ; \n");  
                    kernel_string += string("  int ang_index_k = ((ang_index >> " ) + num2str(plan->logN1)+ string(") & "+num2str(plan->N2-1)+ ");\n");
                    kernel_string += string("  int ang_index_i = (ang_index & " ) + num2str(plan->N1-1)+ string(");\n");
1083
                    kernel_string += string("  cos_sinLUT2("+varRes+",dir,ang_index_i,ang_index_k,cossinLUT1,cossinLUT2);\n");
1084
1085
1086
1087
1088
1089
1090
1091
                    kernel_string += string("}\n");

                }    
                break;
        }
    }
}

1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
// pedantic : use Taylor series approx to degree 3, on 64 grid points between [0,2pi[

static void
insertSinCosCalcTaylor3(string & kernel_string, cl_fft_plan *plan, int num, int denom , string & expr, string & varRes)
{
  if(denom & (denom-1)) {
    kernel_string += string("ang = dir*(2.0f*M_PI*") + num2str(num) + string("/") + num2str(denom) + string(")*("+expr+");\n");
    kernel_string += varRes+string(" = (float2)(native_cos(ang), native_sin(ang));\n");
  } else {

    switch (num) {
    case 0 :
      kernel_string += string("ang = 0.0f;\n");
      kernel_string += varRes+string(" = (float2)(1.0f, 0.0f);\n");
      break;
    default:
            
      // normalize num,denom while num is even
      while((num % 2 ==0 ) && (denom %2 == 0) ){
        denom >>=1;
        num >>=1;
      }
      // if normalized denom < grid size, pick directly from LUT. 
      if(denom <= plan->N2) {
          kernel_string += string("{ int ang_index = (") + num2str(num) + string(" * ( ") + expr + string(")) & ") + num2str(denom -1)   + string("; \n");
          kernel_string += string("  int k = ang_index * ") + num2str(plan->N2 / denom ) + string(";\n");
          kernel_string += string("  float2 cs =cossin_T_LUT[k];\n");                     
          kernel_string += string("  cs.y *=dir;\n");
          
          kernel_string += varRes + string(" = cs;\n");
          kernel_string += string("}\n");
          
                
      } else {
          kernel_string += string("{ int ang_index = (") + num2str(num) + string(" * ( ") + expr + string(")) & ") + num2str(denom -1)   + string("; \n");
      
          // find nearest bin in grid
      
          int logDenom=0;
          int d = denom;
          while (d > 1) {
              d>>=1;
              logDenom++;
          }
          
1137
          kernel_string += string(" int k = (ang_index +  ") + num2str( 1 <<(logDenom - plan->logN2 -1) ) + string(") >> ") + num2str(logDenom - plan->logN2) +string( ";\n");    
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
          
	      // get cos/sin of grid point from LUT                                                                                                                                                                            

          kernel_string += string(" float2 csx0 =cossin_T_LUT[k];\n");                     
          kernel_string += string(" float2 csx0Transp= (float2)(csx0.y,-csx0.x);\n");
          kernel_string += string(" int    r=ang_index - k * ( ")+ num2str(denom >> plan->logN2) + string(" );\n") ; 

        
	      // calculate distance (with sign) from this grid point
          // DO NOT calculate teh angles here directly and subtract as this will deteriorate precision.
          // precompute h0=2*pi/denom;
          float pi2=0x1.921fb54442d18p+2;
          char tw[200];
          sprintf(tw,"%A",-pi2/(float) denom);
          kernel_string += string("  float mh=") +string(tw)+string("*(float)r;\n"); 

          // compute taylor series terms to order 3 and add them up, in "reverse" order (highest order first)

          kernel_string += string("  float mhsqr2= mh*mh*(-0.5f);\n"); 
          kernel_string += string("  float hqub6= mhsqr2*mh*(1.0f/3.0f);\n"); 
          kernel_string += string("  float2 cs;\n"); // we could use varRes in the first place here..
          kernel_string += string("   cs= hqub6 * csx0Transp;\n");

          kernel_string += string("   cs += mhsqr2*csx0;\n");

          kernel_string += string("   cs += mh*csx0Transp;\n");

          kernel_string += string("   cs += csx0;\n");

          kernel_string += string("   cs.y *=dir;\n");

          kernel_string += varRes + string(" = cs;\n");
          kernel_string += string("}\n");

      }
      break;
    }
  }    
}

static void 
insertSinCos(string & kernel_string, cl_fft_plan *plan, int num, int denom , string & expr, string & varRes) 
{
    switch (plan->twiddleMethod) {    
        case clFFT_sincosfunc : insertSinCosCalcDirect(kernel_string,plan,num,denom,expr,varRes); break;
        case clFFT_BigLUT     : insertSinCosCalc(kernel_string,plan,num,denom,expr,varRes); break;
        case clFFT_TaylorLUT  : insertSinCosCalcTaylor3(kernel_string,plan,num,denom,expr,varRes); break;
        default               : insertSinCosCalcDirectNative(kernel_string,plan,num,denom,expr,varRes); break;    
    }    
}

1189

1190

1191

1192
1193
static void
createGlobalFFTKernelString(cl_fft_plan *plan, int n, int BS, cl_fft_kernel_dir dir, int vertBS)
Oliver Bock's avatar
Oliver Bock committed
1194
{
Oliver Bock's avatar
Oliver Bock committed
1195
1196
    int i, j, k, t;
    int radixArr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1197
1198
    int R1Arr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int R2Arr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Oliver Bock's avatar
Oliver Bock committed
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
    int radix, R1, R2;
    int numRadices;

    int maxThreadsPerBlock = plan->max_work_item_per_workgroup;
    int maxArrayLen = plan->max_radix;
    int batchSize = plan->min_mem_coalesce_width;
    clFFT_DataFormat dataFormat = plan->format;
    int vertical = (dir == cl_fft_kernel_x) ? 0 : 1;

    getGlobalRadixInfo(n, radixArr, R1Arr, R2Arr, &numRadices);

    int numPasses = numRadices;

    string localString(""), kernelName("");
    string *kernelString = plan->kernel_string;
    cl_fft_kernel_info **kInfo = &plan->kernel_info;
    int kCount = 0;

    while(*kInfo)
    {
        kInfo = &(*kInfo)->next;
        kCount++;
    }

    int N = n;
    int m = (int)log2(n);
    int Rinit = vertical ? BS : 1;
    batchSize = vertical ? min(BS, batchSize) : batchSize;
    int passNum;
1228
    
Oliver Bock's avatar
Oliver Bock committed
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
    for(passNum = 0; passNum < numPasses; passNum++)
    {

        localString.clear();
        kernelName.clear();

        radix = radixArr[passNum];
        R1 = R1Arr[passNum];
        R2 = R2Arr[passNum];

        int strideI = Rinit;
        for(i = 0; i < numPasses; i++)
            if(i != passNum)
                strideI *= radixArr[i];

        int strideO = Rinit;
        for(i = 0; i < passNum; i++)
            strideO *= radixArr[i];

        int threadsPerXForm = R2;
        batchSize = R2 == 1 ? plan->max_work_item_per_workgroup : batchSize;
        batchSize = min(batchSize, strideI);
        int threadsPerBlock = batchSize * threadsPerXForm;
        threadsPerBlock = min(threadsPerBlock, maxThreadsPerBlock);
        batchSize = threadsPerBlock / threadsPerXForm;
        assert(R2 <= R1);
        assert(R1*R2 == radix);
        assert(R1 <= maxArrayLen);
        assert(threadsPerBlock <= maxThreadsPerBlock);

        int numIter = R1 / R2;
        int gInInc = threadsPerBlock / batchSize;


        int lgStrideO = log2(strideO);
        int numBlocksPerXForm = strideI / batchSize;
        int numBlocks = numBlocksPerXForm;
        if(!vertical)
            numBlocks *= BS;
        else
            numBlocks *= vertBS;

        kernelName = string("fft") + num2str(kCount);
        *kInfo = (cl_fft_kernel_info *) malloc(sizeof(cl_fft_kernel_info));
        (*kInfo)->kernel = 0;
        if(R2 == 1)
            (*kInfo)->lmem_size = 0;
        else
        {
            if(strideO == 1)
                (*kInfo)->lmem_size = (radix + 1)*batchSize;
            else
                (*kInfo)->lmem_size = threadsPerBlock*R1;
        }
        (*kInfo)->num_workgroups = numBlocks;
1284
        (*kInfo)->num_xforms_per_workgroup = 1;
Oliver Bock's avatar
Oliver Bock committed
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
        (*kInfo)->num_workitems_per_workgroup = threadsPerBlock;
        (*kInfo)->dir = dir;
        if( (passNum == (numPasses - 1)) && (numPasses & 1) )
            (*kInfo)->in_place_possible = 1;
        else
            (*kInfo)->in_place_possible = 0;
        (*kInfo)->next = NULL;
        (*kInfo)->kernel_name = (char *) malloc(sizeof(char)*(kernelName.size()+1));
        strcpy((*kInfo)->kernel_name, kernelName.c_str());

        insertVariables(localString, R1);
1296
1297
1298
1299
        
        if((R2 > 1) || (passNum < (numPasses - 1))) {
            insertLocalSinCosLUT(localString, plan, threadsPerBlock);
        }    
Oliver Bock's avatar
Oliver Bock committed
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
        if(vertical)
        {
            localString += string("xNum = groupId >> ") + num2str((int)log2(numBlocksPerXForm)) + string(";\n");
            localString += string("groupId = groupId & ") + num2str(numBlocksPerXForm - 1) + string(";\n");
            localString += string("indexIn = mad24(groupId, ") + num2str(batchSize) + string(", xNum << ") + num2str((int)log2(n*BS)) + string(");\n");
            localString += string("tid = mul24(groupId, ") + num2str(batchSize) + string(");\n");
            localString += string("i = tid >> ") + num2str(lgStrideO) + string(";\n");
            localString += string("j = tid & ") + num2str(strideO - 1) + string(";\n");
            int stride = radix*Rinit;
            for(i = 0; i < passNum; i++)
                stride *= radixArr[i];
            localString += string("indexOut = mad24(i, ") + num2str(stride) + string(", j + ") + string("(xNum << ") + num2str((int) log2(n*BS)) + string("));\n");
            localString += string("bNum = groupId;\n");
        }
        else
        {
            int lgNumBlocksPerXForm = log2(numBlocksPerXForm);
            localString += string("bNum = groupId & ") + num2str(numBlocksPerXForm - 1) + string(";\n");
            localString += string("xNum = groupId >> ") + num2str(lgNumBlocksPerXForm) + string(";\n");
            localString += string("indexIn = mul24(bNum, ") + num2str(batchSize) + string(");\n");
            localString += string("tid = indexIn;\n");
            localString += string("i = tid >> ") + num2str(lgStrideO) + string(";\n");
            localString += string("j = tid & ") + num2str(strideO - 1) + string(";\n");
            int stride = radix*Rinit;
            for(i = 0; i < passNum; i++)
                stride *= radixArr[i];
            localString += string("indexOut = mad24(i, ") + num2str(stride) + string(", j);\n");
            localString += string("indexIn += (xNum << ") + num2str(m) + string(");\n");
            localString += string("indexOut += (xNum << ") + num2str(m) + string(");\n");
        }

        // Load Data
        int lgBatchSize = log2(batchSize);
        localString += string("tid = lId;\n");
        localString += string("i = tid & ") + num2str(batchSize - 1) + string(";\n");
        localString += string("j = tid >> ") + num2str(lgBatchSize) + string(";\n");
        localString += string("indexIn += mad24(j, ") + num2str(strideI) + string(", i);\n");

        if(dataFormat == clFFT_SplitComplexFormat)
        {
            localString += string("in_real += indexIn;\n");
            localString += string("in_imag += indexIn;\n");
            for(j = 0; j < R1; j++)
                localString += string("a[") + num2str(j) + string("].x = in_real[") + num2str(j*gInInc*strideI) + string("];\n");
            for(j = 0; j < R1; j++)
                localString += string("a[") + num2str(j) + string("].y = in_imag[") + num2str(j*gInInc*strideI) + string("];\n");
        }
        else
        {
            localString += string("in += indexIn;\n");
            for(j = 0; j < R1; j++)
                localString += string("a[") + num2str(j) + string("] = in[") + num2str(j*gInInc*strideI) + string("];\n");
        }

1354

1355
        
Oliver Bock's avatar
Oliver Bock committed
1356
1357
1358
1359
1360
        localString += string("fftKernel") + num2str(R1) + string("(a, dir);\n");

        if(R2 > 1)
        {
            // twiddle
1361
1362
1363
            string expr = string("j");
            string resVar = string("w");

Oliver Bock's avatar
Oliver Bock committed
1364
1365
            for(k = 1; k < R1; k++)
            {
1366
                insertSinCos(localString,plan, k, radix , expr, resVar); 
1367
1368
//                localString += string("ang = dir*(2.0f*M_PI*") + num2str(k) + string("/") + num2str(radix) + string(")*j;\n");
//                localString += string("w = (float2)(native_cos(ang), native_sin(ang));\n");
Oliver Bock's avatar
Oliver Bock committed
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
                localString += string("a[") + num2str(k) + string("] = complexMul(a[") + num2str(k) + string("], w);\n");
            }

            // shuffle
            numIter = R1 / R2;
            localString += string("indexIn = mad24(j, ") + num2str(threadsPerBlock*numIter) + string(", i);\n");
            localString += string("lMemStore = sMem + tid;\n");
            localString += string("lMemLoad = sMem + indexIn;\n");
            for(k = 0; k < R1; k++)
                localString += string("lMemStore[") + num2str(k*threadsPerBlock) + string("] = a[") + num2str(k) + string("].x;\n");
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");
            for(k = 0; k < numIter; k++)
                for(t = 0; t < R2; t++)
                    localString += string("a[") + num2str(k*R2+t) + string("].x = lMemLoad[") + num2str(t*batchSize + k*threadsPerBlock) + string("];\n");
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");
            for(k = 0; k < R1; k++)
                localString += string("lMemStore[") + num2str(k*threadsPerBlock) + string("] = a[") + num2str(k) + string("].y;\n");
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");
            for(k = 0; k < numIter; k++)
                for(t = 0; t < R2; t++)
                    localString += string("a[") + num2str(k*R2+t) + string("].y = lMemLoad[") + num2str(t*batchSize + k*threadsPerBlock) + string("];\n");
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");

            for(j = 0; j < numIter; j++)
                localString += string("fftKernel") + num2str(R2) + string("(a + ") + num2str(j*R2) + string(", dir);\n");
        }

        // twiddle
        if(passNum < (numPasses - 1))
        {
            localString += string("l = ((bNum << ") + num2str(lgBatchSize) + string(") + i) >> ") + num2str(lgStrideO) + string(";\n");
            localString += string("k = j << ") + num2str((int)log2(R1/R2)) + string(";\n");
1401
1402
1403
1404
1405
//            localString += string("ang1 = dir*(2.0f*M_PI/") + num2str(N) + string(")*l;\n");
            
            string varRes=string("w");
            
            
Oliver Bock's avatar
Oliver Bock committed
1406
1407
            for(t = 0; t < R1; t++)
            {
1408
1409
                string expr = string("l * (k + ") + num2str((t%R2)*R1 + (t/R2)) + string(")");
                
1410
                insertSinCos(localString, plan, 1, N , expr, varRes) ;
1411
1412
1413
                
//                localString += string("ang = ang1*(k + ") + num2str((t%R2)*R1 + (t/R2)) + string(");\n");
//                localString += string("w = (float2)(native_cos(ang), native_sin(ang));\n");
Oliver Bock's avatar
Oliver Bock committed
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
                localString += string("a[") + num2str(t) + string("] = complexMul(a[") + num2str(t) + string("], w);\n");
            }
        }

        // Store Data
        if(strideO == 1)
        {

            localString += string("lMemStore = sMem + mad24(i, ") + num2str(radix + 1) + string(", j << ") + num2str((int)log2(R1/R2)) + string(");\n");
            localString += string("lMemLoad = sMem + mad24(tid >> ") + num2str((int)log2(radix)) + string(", ") + num2str(radix+1) + string(", tid & ") + num2str(radix-1) + string(");\n");

            for(int i = 0; i < R1/R2; i++)
                for(int j = 0; j < R2; j++)
                    localString += string("lMemStore[ ") + num2str(i + j*R1) + string("] = a[") + num2str(i*R2+j) + string("].x;\n");
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");
            if(threadsPerBlock >= radix)
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
            {
                for(int i = 0; i < R1; i++)
                localString += string("a[") + num2str(i) + string("].x = lMemLoad[") + num2str(i*(radix+1)*(threadsPerBlock/radix)) + string("];\n");
            }
            else
            {
                int innerIter = radix/threadsPerBlock;
                int outerIter = R1/innerIter;
                for(int i = 0; i < outerIter; i++)
                    for(int j = 0; j < innerIter; j++)
                        localString += string("a[") + num2str(i*innerIter+j) + string("].x = lMemLoad[") + num2str(j*threadsPerBlock + i*(radix+1)) + string("];\n");
            }
Oliver Bock's avatar
Oliver Bock committed
1442
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");
Oliver Bock's avatar
Oliver Bock committed
1443

Oliver Bock's avatar
Oliver Bock committed
1444
1445
1446
1447
1448
            for(int i = 0; i < R1/R2; i++)
                for(int j = 0; j < R2; j++)
                    localString += string("lMemStore[ ") + num2str(i + j*R1) + string("] = a[") + num2str(i*R2+j) + string("].y;\n");
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");
            if(threadsPerBlock >= radix)
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
            {
                for(int i = 0; i < R1; i++)
                    localString += string("a[") + num2str(i) + string("].y = lMemLoad[") + num2str(i*(radix+1)*(threadsPerBlock/radix)) + string("];\n");
            }
            else
            {
                int innerIter = radix/threadsPerBlock;
                int outerIter = R1/innerIter;
                for(int i = 0; i < outerIter; i++)
                    for(int j = 0; j < innerIter; j++)
                        localString += string("a[") + num2str(i*innerIter+j) + string("].y = lMemLoad[") + num2str(j*threadsPerBlock + i*(radix+1)) + string("];\n");
            }
Oliver Bock's avatar
Oliver Bock committed
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
            localString += string("barrier(CLK_LOCAL_MEM_FENCE);\n");

            localString += string("indexOut += tid;\n");
            if(dataFormat == clFFT_SplitComplexFormat) {
                localString += string("out_real += indexOut;\n");
                localString += string("out_imag += indexOut;\n");
                for(k = 0; k < R1; k++)
                    localString += string("out_real[") + num2str(k*threadsPerBlock) + string("] = a[") + num2str(k) + string("].x;\n");
                for(k = 0; k < R1; k++)
                    localString += string("out_imag[") + num2str(k*threadsPerBlock) + string("] = a[") + num2str(k) + string("].y;\n");
            }
            else {
                localString += string("out += indexOut;\n");
                for(k = 0; k < R1; k++)
                    localString += string("out[") + num2str(k*threadsPerBlock) + string("] = a[") + num2str(k) + string("];\n");
            }

        }
        else
        {
            localString += string("indexOut += mad24(j, ") + num2str(numIter*strideO) + string(", i);\n");
            if(dataFormat == clFFT_SplitComplexFormat) {
                localString += string("out_real += indexOut;\n");
                localString += string("out_imag += indexOut;\n");
                for(k = 0; k < R1; k++)
                    localString += string("out_real[") + num2str(((k%R2)*R1 + (k/R2))*strideO) + string("] = a[") + num2str(k) + string("].x;\n");
                for(k = 0; k < R1; k++)
                    localString += string("out_imag[") + num2str(((k%R2)*R1 + (k/R2))*strideO) + string("] = a[") + num2str(k) + string("].y;\n");
            }
            else {
                localString += string("out += indexOut;\n");
                for(k = 0; k < R1; k++)
                    localString += string("out[") + num2str(((k%R2)*R1 + (k/R2))*strideO) + string("] = a[") + num2str(k) + string("];\n");
            }
        }

        insertHeader(*kernelString, kernelName, dataFormat);
        *kernelString += string("{\n");
        if((*kInfo)->lmem_size)
            *kernelString += string("    __local float sMem[") + num2str((*kInfo)->lmem_size) + string("];\n");
        *kernelString += localString;
        *kernelString += string("}\n");

        N /= radix;
        kInfo = &(*kInfo)->next;
        kCount++;
    }
1508
1509
1510
}

void FFT1D(cl_fft_plan *plan, cl_fft_kernel_dir dir)
Oliver Bock's avatar
Oliver Bock committed
1511
{
1512
1513
    unsigned int radixArray[10];
    unsigned int numRadix;
Oliver Bock's avatar
Oliver Bock committed
1514

Oliver Bock's avatar
Oliver Bock committed
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
    switch(dir)
    {
        case cl_fft_kernel_x:
            if(plan->n.x > plan->max_localmem_fft_size)
            {
                createGlobalFFTKernelString(plan, plan->n.x, 1, cl_fft_kernel_x, 1);
            }
            else if(plan->n.x > 1)
            {
                getRadixArray(plan->n.x, radixArray, &numRadix, 0);
                if(plan->n.x / radixArray[0] <= plan->max_work_item_per_workgroup)
                {
                    createLocalMemfftKernelString(plan);
                }
                else
                {
                    getRadixArray(plan->n.x, radixArray, &numRadix, plan->max_radix);
                    if(plan->n.x / radixArray[0] <= plan->max_work_item_per_workgroup)
                        createLocalMemfftKernelString(plan);
                    else
                        createGlobalFFTKernelString(plan, plan->n.x, 1, cl_fft_kernel_x, 1);
                }
            }
            break;

        case cl_fft_kernel_y:
            if(plan->n.y > 1)
                createGlobalFFTKernelString(plan, plan->n.y, plan->n.x, cl_fft_kernel_y, 1);
            break;

        case cl_fft_kernel_z:
            if(plan->n.z > 1)
                createGlobalFFTKernelString(plan, plan->n.z, plan->n.x*plan->n.y, cl_fft_kernel_z, 1);
        default:
            return;
    }
1551
1552
}

1553
For faster browsing, not all history is shown. View entire blame