gint jobdesc_streams_count (gchar *job, gchar *pipeline) { JSON_Value *val; JSON_Object *obj; JSON_Array *array; gsize size, i; gint count, index; gchar *bin, *ptype; val = json_parse_string_with_comments (job); obj = json_value_get_object (val); if (g_str_has_prefix (pipeline, "encoder")) { ptype = "appsrc"; array = json_object_dotget_array (obj, "encoders"); sscanf (pipeline, "encoder.%d", &index); obj = json_array_get_object (array, index); } else if (g_str_has_prefix (pipeline, "source")) { ptype = "appsink"; obj = json_object_get_object (obj, "source"); } array = json_object_dotget_array (obj, "bins"); size = json_array_get_count (array); count = 0; for (i = 0; i < size; i++) { bin = (gchar *)json_array_get_string (array, i); if (g_strrstr (bin, ptype) != NULL) count += 1; } json_value_free (val); return count; }
gchar * jobdesc_element_caps (gchar *job, gchar *element) { JSON_Value *val; JSON_Object *obj; JSON_Array *array; gchar *p, *caps; gint index; val = json_parse_string_with_comments (job); obj = json_value_get_object (val); if (g_str_has_prefix (element, "encoder")) { array = json_object_dotget_array (obj, "encoders"); sscanf (element, "encoder.%d", &index); obj = json_array_get_object (array, index); } else { obj = json_object_get_object (obj, "source"); } p = g_strrstr (element, "elements"); caps = (gchar *)json_object_dotget_string (obj, p); if (caps == NULL) { p = NULL; } else { p = g_strdup (caps); } json_value_free (val); return p; }
//TODO: going to need logic to handle incomplete config files void parse_file( char type, char *json, params_t *par ){ /* Parse file and populate applicable data structures */ uint64_t i; JSON_Value *root_value = NULL; JSON_Object *root_object; JSON_Array *array; if( type == 'f' ) root_value = json_parse_file_with_comments( json ); else root_value = json_parse_string_with_comments( json ); root_object = json_value_get_object( root_value ); par->nQ = (uint64_t) json_object_dotget_number( root_object, "scalars.nq" ); par->L = (uint64_t) json_object_dotget_number( root_object, "scalars.lrgs" ); par->res = (uint64_t) json_object_dotget_number( root_object, "scalars.res" ); par->T = json_object_dotget_number( root_object, "scalars.t" ); par->dt = json_object_dotget_number( root_object, "scalars.dt" ); par->al = (double *)malloc( (par->nQ)*sizeof(double) ); par->de = (double *)malloc( (par->nQ)*sizeof(double) ); par->be = (double *)malloc( ((par->nQ)*((par->nQ)-1)/2)*sizeof(double) ); array = json_object_dotget_array( root_object, "coefficients.alpha" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ (par->al)[i] = -json_array_get_number( array, i ); } } array = json_object_dotget_array( root_object, "coefficients.beta" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ (par->be)[i] = -json_array_get_number( array, i ); } } array = json_object_dotget_array( root_object, "coefficients.delta" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ (par->de)[i] = -json_array_get_number( array, i ); } } json_value_free( root_value ); }
/* Testing correctness of parsed values */ void test_suite_2(void) { JSON_Value *root_value; JSON_Object *object; JSON_Array *array; int i; const char *filename = "tests/test_2.txt"; printf("Testing %s:\n", filename); root_value = json_parse_file(filename); TEST(root_value); TEST(json_value_get_type(root_value) == JSONObject); object = json_value_get_object(root_value); TEST(STREQ(json_object_get_string(object, "string"), "lorem ipsum")); TEST(STREQ(json_object_get_string(object, "utf string"), "lorem ipsum")); TEST(json_object_get_number(object, "positive one") == 1.0); TEST(json_object_get_number(object, "negative one") == -1.0); TEST(json_object_get_number(object, "hard to parse number") == -0.000314); TEST(json_object_get_boolean(object, "boolean true") == 1); TEST(json_object_get_boolean(object, "boolean false") == 0); TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull); array = json_object_get_array(object, "string array"); if (array != NULL && json_array_get_count(array) > 1) { TEST(STREQ(json_array_get_string(array, 0), "lorem")); TEST(STREQ(json_array_get_string(array, 1), "ipsum")); } else { tests_failed++; } array = json_object_get_array(object, "x^2 array"); if (array != NULL) { for (i = 0; i < json_array_get_count(array); i++) { TEST(json_array_get_number(array, i) == (i * i)); } } else { tests_failed++; } TEST(json_object_get_array(object, "non existent array") == NULL); TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str")); TEST(json_object_dotget_boolean(object, "object.nested true") == 1); TEST(json_object_dotget_boolean(object, "object.nested false") == 0); TEST(json_object_dotget_value(object, "object.nested null") != NULL); TEST(json_object_dotget_number(object, "object.nested number") == 123); TEST(json_object_dotget_value(object, "should.be.null") == NULL); TEST(json_object_dotget_value(object, "should.be.null.") == NULL); TEST(json_object_dotget_value(object, ".") == NULL); TEST(json_object_dotget_value(object, "") == NULL); array = json_object_dotget_array(object, "object.nested array"); if (array != NULL && json_array_get_count(array) > 1) { TEST(STREQ(json_array_get_string(array, 0), "lorem")); TEST(STREQ(json_array_get_string(array, 1), "ipsum")); } else { tests_failed++; } TEST(json_object_dotget_boolean(object, "nested true")); json_value_free(root_value); }
/* Testing correctness of parsed values */ void test_suite_2(JSON_Value *root_value) { JSON_Object *root_object; JSON_Array *array; size_t i; TEST(root_value); TEST(json_value_get_type(root_value) == JSONObject); root_object = json_value_get_object(root_value); TEST(STREQ(json_object_get_string(root_object, "string"), "lorem ipsum")); TEST(STREQ(json_object_get_string(root_object, "utf string"), "lorem ipsum")); TEST(STREQ(json_object_get_string(root_object, "utf-8 string"), "あいうえお")); TEST(STREQ(json_object_get_string(root_object, "surrogate string"), "lorem𝄞ipsum𝍧lorem")); TEST(json_object_get_number(root_object, "positive one") == 1.0); TEST(json_object_get_number(root_object, "negative one") == -1.0); TEST(json_object_get_number(root_object, "hard to parse number") == -0.000314); TEST(json_object_get_boolean(root_object, "boolean true") == 1); TEST(json_object_get_boolean(root_object, "boolean false") == 0); TEST(json_value_get_type(json_object_get_value(root_object, "null")) == JSONNull); array = json_object_get_array(root_object, "string array"); if (array != NULL && json_array_get_count(array) > 1) { TEST(STREQ(json_array_get_string(array, 0), "lorem")); TEST(STREQ(json_array_get_string(array, 1), "ipsum")); } else { tests_failed++; } array = json_object_get_array(root_object, "x^2 array"); if (array != NULL) { for (i = 0; i < json_array_get_count(array); i++) { TEST(json_array_get_number(array, i) == (i * i)); } } else { tests_failed++; } TEST(json_object_get_array(root_object, "non existent array") == NULL); TEST(STREQ(json_object_dotget_string(root_object, "object.nested string"), "str")); TEST(json_object_dotget_boolean(root_object, "object.nested true") == 1); TEST(json_object_dotget_boolean(root_object, "object.nested false") == 0); TEST(json_object_dotget_value(root_object, "object.nested null") != NULL); TEST(json_object_dotget_number(root_object, "object.nested number") == 123); TEST(json_object_dotget_value(root_object, "should.be.null") == NULL); TEST(json_object_dotget_value(root_object, "should.be.null.") == NULL); TEST(json_object_dotget_value(root_object, ".") == NULL); TEST(json_object_dotget_value(root_object, "") == NULL); array = json_object_dotget_array(root_object, "object.nested array"); if (array != NULL && json_array_get_count(array) > 1) { TEST(STREQ(json_array_get_string(array, 0), "lorem")); TEST(STREQ(json_array_get_string(array, 1), "ipsum")); } else { tests_failed++; } TEST(json_object_dotget_boolean(root_object, "nested true")); TEST(STREQ(json_object_get_string(root_object, "/**/"), "comment")); TEST(STREQ(json_object_get_string(root_object, "//"), "comment")); }
gint jobdesc_encoders_count (gchar *job) { JSON_Value *val; JSON_Object *obj; JSON_Array *encoders; gint count; val = json_parse_string_with_comments (job); obj = json_value_get_object (val); encoders = json_object_dotget_array (obj, "encoders"); count = json_array_get_count (encoders); json_value_free (val); return count; }
gchar * jobdesc_udpstreaming (gchar *job, gchar *pipeline) { JSON_Value *val; JSON_Object *obj; JSON_Array *array; gint index; gchar *p, *udpstreaming; val = json_parse_string_with_comments (job); obj = json_value_get_object (val); array = json_object_dotget_array (obj, "encoders"); sscanf (pipeline, "encoder.%d", &index); obj = json_array_get_object (array, index); p = (gchar *)json_object_get_string (obj, "udpstreaming"); if (p == NULL) { udpstreaming = NULL; } else { udpstreaming = g_strdup (p); } json_value_free (val); return udpstreaming; }
/*------------------------------------------------------------------ - Config file format - simplify, don't need xml, but like the structure { "scalars" : { "nq" : 3, "lrgs" : 4, "print" : true "t" : 10.0, "dt" : 0.1 }, "coefficients" : { "alpha" : [0.112, 0.234, 0.253], "beta" : [0.453, 0.533, -0.732, 0.125, -0.653, 0.752], "delta" : [1.0, 1.0, 1.0] } } ------------------------------------------------------------------*/ int main( int argc, char **argv ){ double *hz, *hhxh; /* hamiltonian components */ double *al, *be, *de; fftw_complex *psi; /* State vector */ fftw_complex factor; double T = 10.0, dt = 0.1; uint64_t i, j, k, bcount; uint64_t nQ=3, N, L=4, dim; int *fft_dims, prnt=0; uint64_t testi, testj; int dzi, dzj; //TODO: consider using smaller vars for flags and these fftw_plan plan; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Parse configuration file - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ //TODO: going to need logic to handle incomplete config files if( argc < 2 ){ fprintf( stderr, "Need a json configuration file. Terminating...\n" ); return 1; } /* Parse file and populate applicable data structures */ { JSON_Value *root_value = NULL; JSON_Object *root_object; JSON_Array *array; root_value = json_parse_file_with_comments( argv[1] ); root_object = json_value_get_object( root_value ); nQ = (uint64_t) json_object_dotget_number( root_object, "scalars.nq" ); prnt = json_object_dotget_boolean( root_object, "scalars.print" ); L = (uint64_t) json_object_dotget_number( root_object, "scalars.lrgs" ); T = json_object_dotget_number( root_object, "scalars.t" ); dt = json_object_dotget_number( root_object, "scalars.dt" ); al = (double *)malloc( nQ*sizeof(double) ); de = (double *)malloc( nQ*sizeof(double) ); be = (double *)malloc( (nQ*(nQ-1)/2)*sizeof(double) ); array = json_object_dotget_array( root_object, "coefficients.alpha" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ al[i] = -json_array_get_number( array, i ); } } array = json_object_dotget_array( root_object, "coefficients.beta" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ be[i] = -json_array_get_number( array, i ); } } array = json_object_dotget_array( root_object, "coefficients.delta" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ de[i] = -json_array_get_number( array, i ); } } json_value_free( root_value ); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Compute the Hamiltonian and state vector for the simulation - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Create state vector and initialize to 1/sqrt(2^n)*(|00...0> + ... + |11...1>) TODO: keep track of local size and local base */ dim = 1 << nQ; factor = 1.0/sqrt( dim ); fft_dims = (int *)malloc( nQ*sizeof(int) ); psi = (fftw_complex *)malloc( (dim)*sizeof(fftw_complex) ); hz = (double *)calloc( (dim),sizeof(double) ); hhxh = (double *)calloc( (dim),sizeof(double) ); for( i = 0; i < nQ; i++ ){ fft_dims[i] = 2; } plan = fftw_plan_dft( nQ, fft_dims, psi, psi, FFTW_FORWARD, FFTW_MEASURE ); /* Assemble Hamiltonian and state vector */ for( k = 0; k < dim; k++ ){ //TODO: when parallelized, k in dzi test will be ~(k + base) bcount = 0; for( i = 0; i < nQ; i++ ){ testi = 1 << (nQ - i - 1); dzi = ((k/testi) % 2 == 0) ? 1 : -1; hz[k] += al[i] * dzi; hhxh[k] += de[i] * dzi; for( j = i; j < nQ; j++ ){ testj = 1 << (nQ - j - 1); dzj = ((k/testj) % 2 == 0) ? 1 : -1; hz[k] += be[bcount] * dzi * dzj; bcount++; } } psi[k] = factor; } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Run the Simulation - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ fftw_complex cz, cx; double t; N = (uint64_t)(T / dt); for( i = 0; i < N; i++ ){ t = i*dt; //t0 = (i-1)*dt; //Time-dependent coefficients cz = (-dt * I)*t/(2.0*T); cx = (-dt * I)*(1 - t/T); //Evolve system expMatTimesVec( psi, hz, cz, dim ); //apply Z part fftw_execute( plan ); expMatTimesVec( psi, hhxh, cx, dim ); //apply X part fftw_execute( plan ); expMatTimesVec( psi, hz, cz, dim ); //apply Z part /* TODO: can probably get some minor speedup by incorporating this into expMatTimesVec if needed */ scaleVec( psi, 1.0/dim, dim ); } fftw_destroy_plan( plan ); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Check solution and clean up - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ //TODO: locally, collect all local largests on one // node, find k largest from that subset if( prnt && nQ < 6 ){ for( i = 0; i < dim; i++ ){ printf( "psi[%d] = (%f, %f)\t%f\n", i, creal( psi[i] ), cimag( psi[i] ), cabs( psi[i]*psi[i] ) ); } } else { uint64_t *largest = (uint64_t *)calloc( L, sizeof(uint64_t) ); findLargest( largest, psi, dim, L ); for( i = 0; i < L; ++i ){ printf( "psi[%d] = (%f, %f)\t%f\n", i, creal( psi[largest[L-1-i]] ), cimag( psi[largest[L-1-i]] ), cabs( psi[largest[L-1-i]]*psi[largest[L-1-i]] ) ); } free( largest ); } /* Free work space. */ fftw_free( psi ); free( fft_dims ); free( hz ); free( hhxh ); return 0; }
int main(){ int i, nQ, lrgs; double t, dt; double *al, *be, *de; JSON_Value *root_value = NULL; JSON_Object *root_object; //JSON_Object *scalar_object; //JSON_Object *coeff_object; JSON_Array *array; root_value = json_parse_file_with_comments( "config.json" ); root_object = json_value_get_object( root_value ); //scalar_object = json_value_get_object( root_object, ); //coeff_object = json_value_get_object( root_value ); nQ = (int)json_object_dotget_number( root_object, "scalars.NQ" ); lrgs = (int)json_object_dotget_number( root_object, "scalars.LRGS" ); t = json_object_dotget_number( root_object, "scalars.T" ); dt = json_object_dotget_number( root_object, "scalars.DT" ); al = (double *)malloc( nQ*sizeof(double) ); be = (double *)malloc( ((nQ*(nQ-1))/2)*sizeof(double) ); de = (double *)malloc( nQ*sizeof(double) ); array = json_object_dotget_array( root_object, "coefficients.ALPHA" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ al[i] = json_array_get_number( array, i ); } } array = json_object_dotget_array( root_object, "coefficients.BETA" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ be[i] = json_array_get_number( array, i ); } } array = json_object_dotget_array( root_object, "coefficients.DELTA" ); if( array != NULL ){ for( i = 0; i < json_array_get_count(array); i++ ){ de[i] = json_array_get_number( array, i ); } } json_value_free(root_value); printf("nQ = %d\n", nQ); printf("lrgs = %d\n", lrgs); printf("t = %f\n", t); printf("dt = %f\n\n", dt); for( i = 0; i < nQ; i++ ){ printf("al[%d] = %f ", i, al[i]); } printf("\n\n"); for( i = 0; i < (nQ*nQ-nQ)/2; i++ ){ printf("be[%d] = %f ", i, be[i]); } printf("\n\n"); for( i = 0; i < nQ; i++ ){ printf("de[%d] = %f ", i, de[i]); } printf("\n\n"); free(al); free(be); free(de); return 0; }
/** * * @property: (in): encoders.x.elements.element.property.name or source.elements.element.property.name */ gchar * jobdesc_element_property_value (gchar *job, gchar *property) { JSON_Value *val; JSON_Object *obj; JSON_Array *array; JSON_Value_Type type; JSON_Value *value; gchar *p; gint64 i; gdouble n; gint index; val = json_parse_string_with_comments (job); obj = json_value_get_object (val); if (g_str_has_prefix (property, "encoder")) { array = json_object_dotget_array (obj, "encoders"); sscanf (property, "encoder.%d", &index); obj = json_array_get_object (array, index); p = g_strrstr (property, "elements"); value = json_object_dotget_value (obj, p); } else if (g_str_has_prefix (property, "source")) { value = json_object_dotget_value (obj, property); } if (value == NULL) { json_value_free (val); return NULL; } type = json_value_get_type (value); switch (type) { case JSONString: p = g_strdup (json_value_get_string (value)); break; case JSONNumber: n = json_value_get_number (value); i = n; if (i == n) { p = g_strdup_printf ("%ld", i); } else { p = g_strdup_printf ("%f", n); } break; case JSONBoolean: if (json_value_get_boolean (value)) { p = g_strdup ("TRUE"); } else { p = g_strdup ("FALSE"); } break; default: GST_ERROR ("property value invalid."); } json_value_free (val); return p; }