Guest User

Untitled

a guest
Nov 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/stat.h>
  6. #include <sys/time.h>
  7.  
  8. int gen_input(int SIZE, float *input_matrix) {
  9. time_t t;
  10. srand((unsigned) time(&t));
  11. for (int i = 0; i < SIZE; i++)
  12. input_matrix[i] = 1000 * (1.0 * rand()) / RAND_MAX;
  13. return 0;
  14. }
  15.  
  16. int transpose(int INPUT_SIZE, int BLOCK_SIZE, float *input_matrix, float *output_matrix) {
  17. for (int i = 0; i < INPUT_SIZE; i += BLOCK_SIZE)
  18. for (int j = 0; j < INPUT_SIZE; j += BLOCK_SIZE)
  19. for (int x = i; x < BLOCK_SIZE + i; x++)
  20. for (int y = j; y < BLOCK_SIZE + j; y++)
  21. output_matrix[y * INPUT_SIZE + x] = input_matrix[x * INPUT_SIZE + y];
  22. return 0;
  23. }
  24.  
  25. int print_matrix(int INPUT_SIZE, float *matrix, int SIZE) {
  26. int counter = 1;
  27. int max_spaces = 12;
  28. for (int i = 0; i < SIZE; i++) {
  29. if (counter == INPUT_SIZE) {
  30. printf("%*f\n", max_spaces, matrix[i]);
  31. counter = 1;
  32. } else {
  33. printf("%*f ", max_spaces, matrix[i]);
  34. counter++;
  35. }
  36. }
  37. return 0;
  38. }
  39.  
  40. int main(int argc, char **argv) {
  41. int INPUT_SIZE = atoi(argv[1]);
  42. int BLOCK_SIZE = atoi(argv[2]);
  43. int SIZE = INPUT_SIZE * INPUT_SIZE;
  44.  
  45. float *input_matrix = (float *) malloc(SIZE * sizeof(float));
  46. float *output_matrix = (float *) malloc(SIZE * sizeof(float));
  47.  
  48. gen_input(SIZE, input_matrix);
  49.  
  50. printf("\n");
  51. print_matrix(INPUT_SIZE, input_matrix, SIZE);
  52. printf("\n");
  53. transpose(INPUT_SIZE, BLOCK_SIZE, input_matrix, output_matrix);
  54. printf("\n");
  55. print_matrix(INPUT_SIZE, output_matrix, SIZE);
  56.  
  57. free(input_matrix);
  58. free(output_matrix);
  59.  
  60. return 0;
  61. }
Add Comment
Please, Sign In to add comment