1 /*
3 BLIS
4 An object-based framework for developing high-performance BLAS-like
5 libraries.
7 Copyright (C) 2014, The University of Texas at Austin
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 - Neither the name of The University of Texas at Austin nor the names
18 of its contributors may be used to endorse or promote products
19 derived from this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
35 #include "blis.h"
36 #include "test_libblis.h"
39 // Static variables.
40 static char* op_str = "subv";
41 static char* o_types = "vv"; // x y
42 static char* p_types = "c"; // conjx
43 static thresh_t thresh[BLIS_NUM_FP_TYPES] = { { 1e-04, 1e-05 }, // warn, pass for s
44 { 1e-04, 1e-05 }, // warn, pass for c
45 { 1e-13, 1e-14 }, // warn, pass for d
46 { 1e-13, 1e-14 } }; // warn, pass for z
48 // Local prototypes.
49 void libblis_test_subv_deps( test_params_t* params,
50 test_op_t* op );
52 void libblis_test_subv_experiment( test_params_t* params,
53 test_op_t* op,
54 iface_t iface,
55 num_t datatype,
56 char* pc_str,
57 char* sc_str,
58 unsigned int p_cur,
59 perf_t* perf,
60 double* resid );
62 void libblis_test_subv_impl( iface_t iface,
63 obj_t* x,
64 obj_t* y );
66 void libblis_test_subv_check( obj_t* alpha,
67 obj_t* beta,
68 obj_t* x,
69 obj_t* y,
70 double* resid );
74 void libblis_test_subv_deps( test_params_t* params, test_op_t* op )
75 {
76 libblis_test_setv( params, &(op->ops->setv) );
77 libblis_test_normfv( params, &(op->ops->normfv) );
78 }
82 void libblis_test_subv( test_params_t* params, test_op_t* op )
83 {
85 // Return early if this test has already been done.
86 if ( op->test_done == TRUE ) return;
88 // Return early if operation is disabled.
89 if ( op->op_switch == DISABLE_ALL ||
90 op->ops->l1v_over == DISABLE_ALL ) return;
92 // Call dependencies first.
93 if ( TRUE ) libblis_test_subv_deps( params, op );
95 // Execute the test driver for each implementation requested.
96 if ( op->front_seq == ENABLE )
97 {
98 libblis_test_op_driver( params,
99 op,
100 BLIS_TEST_SEQ_FRONT_END,
101 op_str,
102 p_types,
103 o_types,
104 thresh,
105 libblis_test_subv_experiment );
106 }
107 }
111 void libblis_test_subv_experiment( test_params_t* params,
112 test_op_t* op,
113 iface_t iface,
114 num_t datatype,
115 char* pc_str,
116 char* sc_str,
117 unsigned int p_cur,
118 perf_t* perf,
119 double* resid )
120 {
121 double time_min = 1e9;
122 double time;
124 dim_t m;
126 conj_t conjx;
128 obj_t alpha, beta;
129 obj_t x, y;
132 // Map the dimension specifier to an actual dimension.
133 m = libblis_test_get_dim_from_prob_size( op->dim_spec[0], p_cur );
135 // Map parameter characters to BLIS constants.
136 bli_param_map_char_to_blis_conj( pc_str[0], &conjx );
138 // Create test scalars.
139 bli_obj_scalar_init_detached( datatype, &alpha );
140 bli_obj_scalar_init_detached( datatype, &beta );
142 // Create test operands (vectors and/or matrices).
143 libblis_test_vobj_create( params, datatype, sc_str[0], m, &x );
144 libblis_test_vobj_create( params, datatype, sc_str[1], m, &y );
146 // Initialize alpha and beta.
147 bli_setsc( 1.0, 1.0, &alpha );
148 bli_setsc( 3.0, 3.0, &beta );
150 // Set x and y to alpha and beta, respectively.
151 bli_setv( &alpha, &x );
152 bli_setv( &beta, &y );
154 // Apply the parameters.
155 bli_obj_set_conj( conjx, x );
157 // Disable repeats since bli_copyv() is not yet tested.
158 //for ( i = 0; i < n_repeats; ++i )
159 {
160 time = bli_clock();
162 libblis_test_subv_impl( iface, &x, &y );
164 time_min = bli_clock_min_diff( time_min, time );
165 }
167 // Estimate the performance of the best experiment repeat.
168 perf->gflops = ( 2.0 * m ) / time_min / FLOPS_PER_UNIT_PERF;
169 if ( bli_obj_is_complex( x ) ) perf->gflops *= 2.0;
170 perf->time = time_min;
172 // Perform checks.
173 libblis_test_subv_check( &alpha, &beta, &x, &y, resid );
175 // Zero out performance and residual if output vector is empty.
176 libblis_test_check_empty_problem( &y, perf, resid );
178 // Free the test objects.
179 bli_obj_free( &x );
180 bli_obj_free( &y );
181 }
185 void libblis_test_subv_impl( iface_t iface,
186 obj_t* x,
187 obj_t* y )
188 {
189 switch ( iface )
190 {
191 case BLIS_TEST_SEQ_FRONT_END:
192 bli_subv( x, y );
193 break;
195 default:
196 libblis_test_printf_error( "Invalid interface type.\n" );
197 }
198 }
202 void libblis_test_subv_check( obj_t* alpha,
203 obj_t* beta,
204 obj_t* x,
205 obj_t* y,
206 double* resid )
207 {
208 num_t dt = bli_obj_datatype( *x );
209 num_t dt_real = bli_obj_datatype_proj_to_real( *x );
210 dim_t m = bli_obj_vector_dim( *x );
212 conj_t conjx = bli_obj_conj_status( *x );
214 obj_t aminusb;
215 obj_t alpha_conj;
216 obj_t norm_r, m_r, temp_r;
218 double junk;
220 //
221 // Pre-conditions:
222 // - x is set to alpha.
223 // - y_orig is set to beta.
224 // Note:
225 // - alpha and beta should have non-zero imaginary components in the
226 // complex cases in order to more fully exercise the implementation.
227 //
228 // Under these conditions, we assume that the implementation for
229 //
230 // y := y_orig - conjx(x)
231 //
232 // is functioning correctly if
233 //
234 // normfv(y) - sqrt( absqsc( beta - conjx(alpha) ) * m )
235 //
236 // is negligible.
237 //
239 bli_obj_scalar_init_detached( dt, &aminusb );
240 bli_obj_scalar_init_detached( dt_real, &temp_r );
241 bli_obj_scalar_init_detached( dt_real, &norm_r );
242 bli_obj_scalar_init_detached( dt_real, &m_r );
244 bli_obj_scalar_init_detached_copy_of( dt, conjx, alpha, &alpha_conj );
246 bli_normfv( y, &norm_r );
248 bli_copysc( beta, &aminusb );
249 bli_subsc( &alpha_conj, &aminusb );
251 bli_setsc( ( double )m, 0.0, &m_r );
253 bli_absqsc( &aminusb, &temp_r );
254 bli_mulsc( &m_r, &temp_r );
255 bli_sqrtsc( &temp_r, &temp_r );
256 bli_subsc( &temp_r, &norm_r );
258 bli_getsc( &norm_r, resid, &junk );
259 }