Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <fftw3.h>
  3. #include <cufft.h>
  4. #include <complex>
  5. #include <cuda_runtime.h>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     /*
  11.      * the matrix is 12 rows x 8 cols, and each element is a 4-float vector
  12.      */
  13.     int max_rows = 12, max_cols = 8;
  14.     const int half_cols = max_cols / 2 + 1;
  15.    
  16.     float *tmp = new float[max_rows * (max_cols + 2) * 4];
  17.     //declare the dimensions
  18.     int dims[2] = {max_rows, max_cols};
  19.     //initialize the fftw plan
  20.     const fftwf_plan forwards =
  21.             fftwf_plan_many_dft_r2c(2, dims, 4, tmp, 0,
  22.                                                             4, 1,
  23.                                                             reinterpret_cast<fftwf_complex *>(tmp), 0,
  24.                                                             4, 1, FFTW_PATIENT);
  25.     //initialize the matrix m      
  26.     float *m = new float[max_rows * (half_cols * 2) * 4];
  27.     for(int y = 0; y < max_rows; y++) {
  28.         for(int x = 0; x < (half_cols * 2); x++) {
  29.             for(int i = 0; i < 4; i++) {
  30.                 *(m + (y * (half_cols * 2) + x) * 4 + i) = (y * (half_cols * 2) + x) * 4 + i;
  31.             }
  32.         }
  33.     }
  34.     //output original matrix
  35.     cout << "the initial matrix:" << endl;
  36.     for(int y = 0; y < max_rows; y++) {
  37.         for(int x = 0; x < (half_cols * 2); x++) {
  38.             for(int i = 0; i < 4; i++) {
  39.                 cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
  40.             }
  41.         }
  42.         cout << endl;
  43.     }
  44.     cout << endl;
  45.     //do the fft with fftw
  46.     fftwf_execute_dft_r2c(forwards, reinterpret_cast<float *>(m),
  47.                                                 reinterpret_cast<fftwf_complex *>(m));
  48.    
  49.     //out the matrix values after transform
  50.     cout << "the transformed matrix(FFTW3):" << endl;
  51.     for(int y = 0; y < max_rows; y++) {
  52.         for(int x = 0; x < (half_cols * 2); x++) {
  53.             for(int i = 0; i < 4; i++) {
  54.                 cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
  55.             }
  56.         }
  57.         cout << endl;
  58.     }
  59.     cout << endl;
  60.     //--------------------------------------------------------------------------------------
  61.     //reinitialize the matrix
  62.     for(int y = 0; y < max_rows; y++) {
  63.         for(int x = 0; x < (half_cols * 2); x++) {
  64.             for(int i = 0; i < 4; i++) {
  65.                 *(m + (y * (half_cols * 2) + x) * 4 + i) = (y * (half_cols * 2) + x) * 4 + i;
  66.             }
  67.         }
  68.     }
  69.    
  70.     //output original matrix
  71.     cout << "the initial matrix:" << endl;
  72.     for(int y = 0; y < max_rows; y++) {
  73.         for(int x = 0; x < (half_cols * 2); x++) {
  74.             for(int i = 0; i < 4; i++) {
  75.                 cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
  76.             }
  77.         }
  78.         cout << endl;
  79.     }
  80.     cout << endl;
  81.     cufftHandle plan_forward;
  82.     cufftResult result;
  83.  
  84.     result = cufftPlanMany(&plan_forward, 2, dims, NULL, 4, 1, NULL, 4, 1, CUFFT_R2C, 4);
  85.    
  86.     float *dev_m;
  87.     cudaMalloc((void **)&dev_m, 4 * max_rows * (half_cols * 2) * sizeof(float));
  88.     cudaMemcpy(dev_m, m, 4 * max_rows * (half_cols * 2) * sizeof(float), cudaMemcpyHostToDevice);
  89.    
  90.     result = cufftExecR2C(plan_forward, dev_m, reinterpret_cast<cufftComplex *>(dev_m));
  91.    
  92.     cudaMemcpy(m, dev_m, 4 * max_rows * (half_cols * 2) * sizeof(float), cudaMemcpyDeviceToHost);
  93.  
  94.     //out the matrix values after transform
  95.     cout << "the transformed matrix(CUFFT):" << endl;
  96.     for(int y = 0; y < max_rows; y++) {
  97.         for(int x = 0; x < (half_cols * 2); x++) {
  98.             for(int i = 0; i < 4; i++) {
  99.                 cout << *(m + (y * (half_cols * 2) + x) * 4 + i) << " ";
  100.             }
  101.         }
  102.         cout << endl;
  103.     }
  104.  
  105.    
  106.     return 0;
  107. }
  108. /*
  109. Program output:
  110.  
  111. [root@localhost ~]# ./test
  112. the initial matrix:
  113. 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
  114. 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
  115. 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
  116. 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
  117. 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
  118. 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
  119. 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
  120. 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
  121. 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
  122. 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
  123. 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
  124. 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
  125.  
  126. the transformed matrix(FFTW3):
  127. 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
  128. -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
  129. -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
  130. -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
  131. -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
  132. -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
  133. -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
  134. -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
  135. -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
  136. -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
  137. -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
  138. -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
  139.  
  140. the initial matrix:
  141. 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
  142. 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
  143. 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
  144. 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
  145. 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
  146. 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
  147. 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
  148. 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
  149. 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
  150. 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
  151. 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
  152. 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
  153.  
  154. the transformed matrix(CUFFT):
  155. 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
  156. -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
  157. -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
  158. 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
  159. -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
  160. -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
  161. 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
  162. -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
  163. -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
  164. 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
  165. -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
  166. -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
  167. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement