Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. /*
  2. Copyright (C) 2012 DolphinCommode
  3. This is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mieconsole is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14.  
  15. #include <cuComplex.h>
  16. #include <png.h>
  17. #include <stdio.h>
  18.  
  19. #define iterations (1024)
  20.  
  21. #define resox (2048) //17
  22. #define resoy (resox)
  23.  
  24. #define mag_ratio (1.0f)
  25.  
  26. #define X0 (0.0f)
  27. #define Y0 (0.0f)
  28.  
  29. #define Xshift ((-1.5f/mag_ratio)+X0)
  30. #define Yshift ((-1.5f/mag_ratio)+Y0)
  31.  
  32. #define CXmin (-1.5f/mag_ratio)
  33. #define CXmax (1.5f/mag_ratio)
  34. #define CXshag ((fabsf(CXmin)+fabsf(CXmax))/resox)
  35. #define CYmin (-1.5f/mag_ratio)
  36. #define CYmax (1.5f/mag_ratio)
  37. #define CYshag ((fabsf(CYmin)+fabsf(CYmax))/resoy)
  38.  
  39. #define C make_cuFloatComplex(0.285f, 0.01f)
  40.  
  41. __global__ void eval(unsigned short int *row, unsigned short int x)
  42. {
  43.  int y = threadIdx.x/*, y = threadIdx.y*/;
  44.  
  45.  cuFloatComplex Z = make_cuFloatComplex(y*CYshag+Yshift, x*CXshag+Xshift);
  46.  unsigned short int iter=0;
  47.  
  48.  while(++iter<iterations && cuCabsf(Z) < 4.0f) Z = cuCaddf(cuCmulf(Z, Z), C);
  49.  
  50.  float tmp2 = /*fmodf(*/(iter-log2f(logf(cuCabsf(Z))))/*, 32.0)*/;
  51.  
  52.  row[y*3] = floorf(fmodf((tmp2*1792.0f), 0xFFFF));
  53.  row[y*3] = (row[y*3]<<8) | (row[y*3]>>8);
  54.  
  55.  row[y*3+1] = floorf(fmodf((tmp2*3584.0f), 0xFFFF));
  56.  row[y*3+1] = (row[y*3+1]<<8) | (row[y*3+1]>>8);
  57.  
  58.  row[y*3+2] = floorf(fmodf((tmp2*512.0f), 0xFFFF));
  59.  row[y*3+2] = (row[y*3+2]<<8) | (row[y*3+2]>>8);
  60. }
  61.  
  62. __host__ int main(int argc, char **argv)
  63. {
  64.  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  65.  png_infop info_ptr = png_create_info_struct(png_ptr);
  66.  
  67.  FILE *fp = fopen("out.png", "wb");
  68.  png_init_io(png_ptr, fp);
  69.  png_set_IHDR(png_ptr, info_ptr, resox, resoy, 16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  70.  //png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
  71.  png_write_info(png_ptr, info_ptr);
  72.  
  73.  for(unsigned short int x=0; x<resox; x++)
  74.  {
  75.   unsigned short int *row_device;
  76.   cudaMalloc(&row_device, resox*4*16);
  77.  
  78.   eval<<<1, 512>>>(row_device, x);
  79.  
  80.   cudaEvent_t syncEvent;
  81.   cudaEventCreate(&syncEvent);
  82.   cudaEventRecord(syncEvent, 0);
  83.   cudaEventSynchronize(syncEvent);
  84.  
  85.   unsigned short int row[resox*4];
  86.   cudaMemcpy(row, row_device, resox*4*16, cudaMemcpyDeviceToHost);
  87.   png_write_row(png_ptr, (png_bytep)row);
  88.  
  89.   cudaEventDestroy(syncEvent);
  90.   cudaFree(row_device);
  91.  }
  92.  png_write_end(png_ptr, NULL);
  93.  
  94.  fclose(fp);
  95.  
  96.  return EXIT_SUCCESS;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement