0
votes

I am just testing fftw and cufft but the results are different(I am a beginner for this area). The matrix is 12 rows x 8 cols and each element is a 4-float vector, and the transform is real to complex. I tried to keep the settings of fftw and cufft the same so the results should be the same, but the outputs are different. Could someone please help to figure out what is going wrong? for a syntax highlight version: http://pastebin.com/Fs5B1Ty6

#include <iostream>
#include <fftw3.h>
#include <cufft.h>
#include <complex>
#include <cuda_runtime.h>

using namespace std;

int main() {
    /*
     * the matrix is 12 rows x 8 cols, and each element is a 4-float vector
     */
    int max_rows = 12, max_cols = 8;
    const int half_cols = max_cols / 2 + 1;

    float *tmp = new float[max_rows * (max_cols + 2) * 4];
    //declare the dimensions
    int dims[2] = {max_rows, max_cols};
    //initialize the fftw plan
    const fftwf_plan forwards =
            fftwf_plan_many_dft_r2c(2, dims, 4, tmp, 0,
                                                            4, 1,
                                                            reinterpret_cast<fftwf_complex *>(tmp), 0,
                                                            4, 1, FFTW_PATIENT);
    //initialize the matrix m       
    float *m = new float[max_rows * (half_cols * 2) * 4];
    for(int y = 0; y < max_rows; y++) {
        for(int x = 0; x < (half_cols * 2); x++) {
            for(int i = 0; i < 4; i++) {
                *(m + (y * (half_cols * 2) + x) * 4 + i) = (y * (half_cols * 2) + x) * 4 + i;
            }
        }
    }
    //output original matrix
    cout << "the initial matrix:" << endl;
    for(int y = 0; y < max_rows; y++) {
        for(int x = 0; x < (half_cols * 2); x++) {
            for(int i = 0; i < 4; i++) {
                cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
            }
        }
        cout << endl;
    }
    cout << endl;
    //do the fft with fftw
    fftwf_execute_dft_r2c(forwards, reinterpret_cast<float *>(m),
                                                reinterpret_cast<fftwf_complex *>(m));

    //out the matrix values after transform
    cout << "the transformed matrix(FFTW3):" << endl;
    for(int y = 0; y < max_rows; y++) {
        for(int x = 0; x < (half_cols * 2); x++) {
            for(int i = 0; i < 4; i++) {
                cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
            }
        }
        cout << endl;
    }
    cout << endl;
    //--------------------------------------------------------------------------------------
    //reinitialize the matrix
    for(int y = 0; y < max_rows; y++) {
        for(int x = 0; x < (half_cols * 2); x++) {
            for(int i = 0; i < 4; i++) {
                *(m + (y * (half_cols * 2) + x) * 4 + i) = (y * (half_cols * 2) + x) * 4 + i;
            }
        }
    }

    //output original matrix
    cout << "the initial matrix:" << endl;
    for(int y = 0; y < max_rows; y++) {
        for(int x = 0; x < (half_cols * 2); x++) {
            for(int i = 0; i < 4; i++) {
                cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
            }
        }
        cout << endl;
    }
    cout << endl;
    cufftHandle plan_forward;
    cufftResult result;

    result = cufftPlanMany(&plan_forward, 2, dims, NULL, 4, 1, NULL, 4, 1, CUFFT_R2C, 4);

    float *dev_m;
    cudaMalloc((void **)&dev_m, 4 * max_rows * (half_cols * 2) * sizeof(float));
    cudaMemcpy(dev_m, m, 4 * max_rows * (half_cols * 2) * sizeof(float), cudaMemcpyHostToDevice);

    result = cufftExecR2C(plan_forward, dev_m, reinterpret_cast<cufftComplex *>(dev_m)); 

    cudaMemcpy(m, dev_m, 4 * max_rows * (half_cols * 2) * sizeof(float), cudaMemcpyDeviceToHost);

    //out the matrix values after transform
    cout << "the transformed matrix(CUFFT):" << endl;
    for(int y = 0; y < max_rows; y++) {
        for(int x = 0; x < (half_cols * 2); x++) {
            for(int i = 0; i < 4; i++) {
                cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
            }
        }
        cout << endl;
    }


    return 0;
}

This program can be compiled with:

g++  -I/usr/include -I/usr/local/cuda-6.0/include -L/usr/local/cuda-6.0/lib64 -L/usr/lib64 test.cpp -lcufft -lfftw3f -lcudart -otest

And the output:

[root@localhost ~]# ./test 
the initial matrix:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 

the transformed matrix(FFTW3):
22464 0 22560 0 22656 0 22752 0 -192 463.529 -192 463.529 -192 463.529 -192 463.529 -192 192 -192 192 -192 192 -192 192 -192 79.529 -192 79.529 -192 79.529 -192 79.529 -192 0 -192 0 -192 0 -192 0 
-1920 7165.54 -1920 7165.54 -1920 7165.54 -1920 7165.54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 3325.54 -1920 3325.54 -1920 3325.54 -1920 3325.54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 1920 -1920 1920 -1920 1920 -1920 1920 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 1108.51 -1920 1108.51 -1920 1108.51 -1920 1108.51 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 514.462 -1920 514.462 -1920 514.462 -1920 514.462 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 0 -1920 0 -1920 0 -1920 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 -514.462 -1920 -514.462 -1920 -514.462 -1920 -514.462 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 -1108.51 -1920 -1108.51 -1920 -1108.51 -1920 -1108.51 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 -1920 -1920 -1920 -1920 -1920 -1920 -1920 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 -3325.54 -1920 -3325.54 -1920 -3325.54 -1920 -3325.54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
-1920 -7165.54 -1920 -7165.54 -1920 -7165.54 -1920 -7165.54 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

the initial matrix:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 

the transformed matrix(CUFFT):
5616 0 -48 115.882 -48 48 -48 19.8822 -48 0 -480 1791.38 0 0 0 0 0 -0 3.05176e-05 0.00012207 -480 831.384 0 0 0 0 0 -0 0 6.10352e-05 -480 480 0 0 0 0 0 -0 0 0 
-480 277.128 0 0 0 0 0 -0 0 0 -480 128.616 0 0 0 0 0 -0 -1.52588e-05 1.52588e-05 -480 0 0 0 0 0 0 -0 0 -0 -480 -128.616 0 0 0 0 0 -0 -1.52588e-05 -1.52588e-05 
-480 -277.128 0 0 0 0 0 -0 0 0 -480 -480 0 0 0 0 0 -0 0 0 -480 -831.384 0 0 0 0 0 -0 0 -6.10352e-05 -480 -1791.38 0 0 0 0 0 -0 3.05176e-05 -0.00012207 
17136 0 -48 115.882 -48 48 -48 19.8822 -48 0 -480 1791.38 0 0 0 0 0 -0 3.05176e-05 0.00012207 -480 831.384 0 0 0 0 0 -0 0 6.10352e-05 -480 480 0 0 0 0 0 -0 0 0 
-480 277.128 0 0 0 0 0 -0 0 0 -480 128.616 0 0 0 0 0 -0 -1.52588e-05 1.52588e-05 -480 0 0 0 0 0 0 -0 0 -0 -480 -128.616 0 0 0 0 0 -0 -1.52588e-05 -1.52588e-05 
-480 -277.128 0 0 0 0 0 -0 0 0 -480 -480 0 0 0 0 0 -0 0 0 -480 -831.384 0 0 0 0 0 -0 0 -6.10352e-05 -480 -1791.38 0 0 0 0 0 -0 3.05176e-05 -0.00012207 
28656 0 -48 115.882 -48 48 -48 19.8822 -48 0 -480 1791.38 0 0 0 0 0 -0 3.05176e-05 0.00012207 -480 831.384 0 0 0 0 0 -0 0 6.10352e-05 -480 480 0 0 0 0 0 -0 0 0 
-480 277.128 0 0 0 0 0 -0 0 0 -480 128.616 0 0 0 0 0 -0 -1.52588e-05 1.52588e-05 -480 0 0 0 0 0 0 -0 0 -0 -480 -128.616 0 0 0 0 0 -0 -1.52588e-05 -1.52588e-05 
-480 -277.128 0 0 0 0 0 -0 0 0 -480 -480 0 0 0 0 0 -0 0 0 -480 -831.384 0 0 0 0 0 -0 0 -6.10352e-05 -480 -1791.38 0 0 0 0 0 -0 3.05176e-05 -0.00012207 
40176 0 -48 115.882 -48 48 -48 19.8822 -48 0 -480 1791.38 0 0 0 0 0 -0 3.05176e-05 0.00012207 -480 831.384 0 0 0 0 0 -0 0 6.10352e-05 -480 480 0 0 0 0 0 -0 0 0 
-480 277.128 0 0 0 0 0 -0 0 0 -480 128.616 0 0 0 0 0 -0 -1.52588e-05 1.52588e-05 -480 0 0 0 0 0 0 -0 0 -0 -480 -128.616 0 0 0 0 0 -0 -1.52588e-05 -1.52588e-05 
-480 -277.128 0 0 0 0 0 -0 0 0 -480 -480 0 0 0 0 0 -0 0 0 -480 -831.384 0 0 0 0 0 -0 0 -6.10352e-05 -480 -1791.38 0 0 0 0 0 -0 3.05176e-05 -0.00012207 
1
It looks like you have a factor of 4 in there, and maybe some differences in bin ordering for DC/Nyquist.Paul R
@PaulR How to fix this problem please?Mickey Shine
It may not even be a problem as such - check the documentation for both FFTs in regard to scaling factors and bin ordering etc.Paul R

1 Answers

2
votes

I suggest reading the cufft documentation on Advanced Data Layout where it states:

"Passing inembed or onembed set to NULL is a special case and is equivalent to passing n for each. This is same as the basic data layout and other advanced parameters such as istride are ignored."

However this is what you are doing (passing inembed and onembed as NULL), so it's reasonable to assume the istride and ostride parameters you are passing to cufftPlanMany may not be having the desired effect.

To rectify this, you can set Advanced Data Layout parameters consistent with your interleaved data format.

Specifically, in your case, I was able to achieve approximate parity between fftw and cuFFT results by making the following changes:

int my_inembed[2];
int my_onembed[2];
my_inembed[1] = half_cols*2;
my_onembed[1] = half_cols;
result = cufftPlanMany(&plan_forward, 2, dims, my_inembed, 4, 1, my_onembed, 4, 1, CUFFT_R2C, 4);

The first four lines above are added to your code, the last is a modification of your existing cufftPlanMany call.