File: dotimings.c

package info (click to toggle)
libstb 0.0~git20241109.5c20573%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,520 kB
  • sloc: ansic: 80,776; cpp: 1,506; makefile: 114; sh: 22
file content (224 lines) | stat: -rw-r--r-- 6,256 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _MSC_VER

#define stop() __debugbreak()
#include <windows.h>
#define int64 __int64
#pragma warning(disable:4127)

#define get_milliseconds GetTickCount

#else

#define stop() __builtin_trap()
#define int64 long long

typedef unsigned int U32;
typedef unsigned long long U64;

#include <time.h>
static int get_milliseconds()
{
  struct timespec ts;
  clock_gettime( CLOCK_MONOTONIC, &ts );
  return (U32) ( ( ((U64)(U32)ts.tv_sec) * 1000LL ) + (U64)(((U32)ts.tv_nsec+500000)/1000000) );
}

#endif

#if defined(TIME_SIMD)
  // default for most platforms
#elif defined(TIME_SCALAR)
  #define STBIR_NO_SIMD
#else
  #error You must define TIME_SIMD or TIME_SCALAR when compiling this file.
#endif

#define STBIR_PROFILE
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#define STBIR__V_FIRST_INFO_BUFFER v_info
#include "stb_image_resize2.h"  // new one!

#if defined(TIME_SIMD)  && !defined(STBIR_SIMD)
#error Timing SIMD, but scalar was ON!
#endif

#if defined(TIME_SCALAR)  && defined(STBIR_SIMD)
#error Timing scalar, but SIMD was ON!
#endif

#define HEADER 32


static int file_write( const char *filename, void * buffer, size_t size ) 
{
  FILE * f = fopen( filename, "wb" );
  if ( f == 0 ) return 0;
  if ( fwrite( buffer, 1, size, f) != size ) return 0;
  fclose(f);
  return 1;
}

int64 nresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt )
{
  STBIR_RESIZE resize;
  int t;
  int64 b;

  stbir_resize_init( &resize, i, ix, iy, ip, o, ox, oy, op, buf, type );
  stbir_set_edgemodes( &resize, edg, edg );
  stbir_set_filters( &resize, flt, flt );
  
  stbir_build_samplers_with_splits( &resize, 1 );

  b = 0x7fffffffffffffffULL;
  for( t = 0 ; t < 16 ; t++ )
  {
    STBIR_PROFILE_INFO profile;
    int64 v;
    if(!stbir_resize_extended( &resize ) )
      stop();
    stbir_resize_extended_profile_info( &profile, &resize );
    v = profile.clocks[1]+profile.clocks[2];
    if ( v < b )
    {
      b = v;
      t = 0;
    }
  }

  stbir_free_samplers( &resize );

  return b;
}


#define INSIZES 5
#define TYPESCOUNT 5
#define NUM 64

static const int sizes[INSIZES]={63,126,252,520,772};
static const int types[TYPESCOUNT]={STBIR_1CHANNEL,STBIR_2CHANNEL,STBIR_RGB,STBIR_4CHANNEL,STBIR_RGBA};
static const int effective[TYPESCOUNT]={1,2,3,4,7};

int main( int argc, char ** argv )
{
  unsigned char * input;
  unsigned char * output;
  int dimensionx, dimensiony;
  int scalex, scaley;
  int totalms;
  int timing_count;
  int ir;
  int * file;
  int * ts;
  int64 totalcycles;

  if ( argc != 6 )
  {
    printf("command: dotimings x_samps y_samps x_scale y_scale outfilename\n");
    exit(1);
  }

  input = malloc( 4*1200*1200 );
  memset( input, 0x80, 4*1200*1200 );
  output = malloc( 4*10000*10000ULL );
  
  dimensionx = atoi( argv[1] );
  dimensiony = atoi( argv[2] );
  scalex = atoi( argv[3] );
  scaley = atoi( argv[4] );

  timing_count = dimensionx * dimensiony * INSIZES * TYPESCOUNT;

  file = malloc( sizeof(int) * ( 2 * timing_count + HEADER ) );
  ts = file + HEADER;

  totalms = get_milliseconds();  
  totalcycles = STBIR_PROFILE_FUNC();
  for( ir = 0 ; ir < INSIZES ; ir++ )
  {
    int ix, iy, ty;
    ix = iy = sizes[ir];

    for( ty = 0 ; ty < TYPESCOUNT ; ty++ )
    {
      int h, hh;

      h = 1;
      for( hh = 0 ; hh < dimensiony; hh++ )
      {
        int ww, w = 1;
        for( ww = 0 ; ww < dimensionx; ww++ )
        {
          int64 VF, HF;
          int good;
        
          v_info.control_v_first = 2; // vertical first
          VF = nresize( output, w, h, (w*4*1)&~3, input, ix, iy, ix*4*1, types[ty], STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL );
          v_info.control_v_first = 1; // horizonal first
          HF = nresize( output, w, h, (w*4*1)&~3, input, ix, iy, ix*4*1, types[ty], STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL );

          good = ( ((HF<=VF) && (!v_info.v_first)) || ((VF<=HF) && (v_info.v_first)));

//          printf("\r%d,%d, %d,%d, %d, %I64d,%I64d, // Good: %c(%c-%d)  CompEst: %.1f %.1f\n", ix, iy, w, h, ty, VF, HF, good?'y':'n', v_info.v_first?'v':'h', v_info.v_resize_classification, v_info.v_cost,v_info.h_cost );
          ts[0] = (int)VF;
          ts[1] = (int)HF;

          ts += 2;

          w += scalex;
        }
        printf(".");
        h += scaley;  
      }
    }
  }
  totalms = get_milliseconds() - totalms;  
  totalcycles = STBIR_PROFILE_FUNC() - totalcycles;

  printf("\n");

  file[0] = 'VFT1';

  #if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(__SSE2__) || defined( _M_IX86_FP ) || defined(__i386) || defined( __i386__ ) || defined( _M_IX86 ) || defined( _X86_ )
  file[1] = 1;  // x64
  #elif defined( _M_AMD64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) || defined(__ARM_NEON) || defined(__arm__) || defined( _M_ARM )
  file[1] = 2;  // arm
  #else
  file[1] = 99;  // who knows???
  #endif
  
  #ifdef STBIR_SIMD8
    file[2] = 2;  // simd-8
  #elif defined( STBIR_SIMD )
    file[2] = 1;  // simd-4
  #else
    file[2] = 0;  // nosimd
  #endif
  
  file[3] = dimensionx; // dimx
  file[4] = dimensiony; // dimy
  file[5] = TYPESCOUNT;  // channel types
  file[ 6] = types[0]; file[7] = types[1]; file[8] = types[2]; file[9] = types[3]; file[10] = types[4];  // buffer_type 
  file[11] = effective[0]; file[12] = effective[1]; file[13] = effective[2]; file[14] = effective[3]; file[15] = effective[4];  // effective channels 
  file[16] = INSIZES;  // resizes
  file[17] = sizes[0]; file[18] = sizes[0]; // input sizes (w x h)
  file[19] = sizes[1]; file[20] = sizes[1];
  file[21] = sizes[2]; file[22] = sizes[2];
  file[23] = sizes[3]; file[24] = sizes[3];
  file[25] = sizes[4]; file[26] = sizes[4];
  file[27] = scalex;   file[28] = scaley;  // scale the dimx and dimy amount ( for(i=0;i<dimx) outputx = 1 + i*scalex; )
  file[29] = totalms;
  ((int64*)(file+30))[0] = totalcycles;

  if ( !file_write( argv[5], file, sizeof(int) * ( 2 * timing_count + HEADER ) ) )
    printf( "Error writing file: %s\n", argv[5] );
  else
    printf( "Successfully wrote timing file: %s\n", argv[5] );

  return 0;
}