//************************************************ Arr Arr::operator,(const int intval){ double rtnArr[M*N]; for (int ii = 0; ii < M*N;ii++){ rtnArr[ii]=val[ii]*intval; } return Arr (rtnArr,M,N); }
//************************************************ Arr Arr::operator*(const Arr& obj){ //---------------------------------------------// // Multiple A by B // Inputs: // A: MxK matrix // B: K+N matrix // M: size of A // N: size of B // K: size shared by A and B // Returns: // C: MxN matrix //---------------------------------------------// int K = obj.M; int N_input = obj.N; int M_input = N; if (obj.M!=M){ cout << "Likely problem with matrix, please check results\n"; } double A_tmp[K*M_input];for(int ii =0;ii<K*M_input;ii++){A_tmp[ii]=val[ii];} double B_tmp[K*N_input];for(int ii =0;ii<K*N_input;ii++){B_tmp[ii]=obj.val[ii];} double C_rtn[M_input*N_input];for(int ii =0;ii<<M_input*N_input;ii++){C_rtn[ii]=0;} char transa = 'n'; char transb = 't'; double alpha =1;double beta = 0; dgemm_(&transa, &transb, &M_input, &N_input, &K,&alpha,A_tmp,&M_input,B_tmp,&N_input,&beta,C_rtn, &K ); return Arr (C_rtn,M_input,N_input); }
//************************************************ Arr times(const double intval, const Arr& obj){ double rtnArr[obj.M*obj.N]; for (int ii = 0; ii < obj.M*obj.N;ii++){ rtnArr[ii]=obj.val[ii]*intval; } return Arr (rtnArr,obj.M,obj.N); }
//************************************************ Arr Arr::operator,(const Arr& obj){ //---------------------------------------------// // muliply every element //---------------------------------------------// double rtnArr[M*N]; if (M*N != obj.M*obj.N) cout << "Size not compatible, results are likely wrong!\n"; for (int ii = 0; ii < M*N;ii++){ rtnArr[ii]=val[ii]*obj.val[ii]; } return Arr (rtnArr,M,N); }
//************************************************ Arr Arr::operator/(const Arr& obj){ //---------------------------------------------// // Matrix solution to Y=M*X //---------------------------------------------// int NRHS = obj.N; int NLHS = obj.M; int *IPIV = new int[N+1]; int INFO; //Create temporary variables so the fortran code doesn't change //input variables double A_tmp[M*N];for(int ii =0;ii<M*N;ii++){A_tmp[ii]=val[ii];} double B_rtn[obj.N*obj.M];for(int ii =0;ii<obj.N*obj.M;ii++){B_rtn[ii]=obj.val[ii];} dgesv_(&NLHS,&NRHS,A_tmp,&NLHS,IPIV,B_rtn,&N,&INFO); delete [] IPIV; return Arr (B_rtn,obj.M,obj.N); }
/** The get-recrod command */ int cmd_get_record (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } if (OPTIONS.host[0] == 0) { fprintf (stderr, "%% No hostid provided\n"); return 1; } var *apires = api_get ("/%s/host/%s", OPTIONS.tenant, OPTIONS.host); if (OPTIONS.json) { var_dump (apires, stdout); var_free (apires); return 0; } #define Arr(x) var_get_array_forkey(apires,x) #define Vint(x) var_get_int_forkey(apires,x) #define Vstr(x) var_get_str_forkey(apires,x) #define Vfrac(x) var_get_double_forkey(apires,x) #define VDint(x,y) var_get_int_forkey(var_get_dict_forkey(apires,x),y) #define VDstr(x,y) var_get_str_forkey(var_get_dict_forkey(apires,x),y) #define VDfrac(x,y) var_get_double_forkey(var_get_dict_forkey(apires,x),y) #define VAfrac(x,y) var_get_double_atindex(var_get_array_forkey(apires,x),y) #define Vdone(x) var_delete_key(apires,x) /* -------------------------------------------------------------*/ print_hdr ("HOST"); print_value ("UUID", "%s", OPTIONS.host); print_value ("Hostname", "%s", Vstr("hostname")); print_value ("Address", "%s", VDstr("agent","ip")); print_value ("Status", "\033[1m%s\033[0m", Vstr("status")); print_array ("Problems", Arr("problems")); Vdone("hostname"); Vdone("agent"); Vdone("status"); Vdone("problems"); char uptimestr[128]; uint64_t uptime = Vint("uptime"); Vdone("uptime"); uint64_t u_days = uptime / 86400ULL; uint64_t u_hours = (uptime - (86400 * u_days)) / 3600ULL; uint64_t u_mins = (uptime - (86400 * u_days) - (3600 * u_hours)) / 60ULL; uint64_t u_sec = uptime % 60; if (u_days) { sprintf (uptimestr, "%llu day%s, %llu:%02llu:%02llu", u_days, (u_days==1)?"":"s", u_hours, u_mins, u_sec); } else if (u_hours) { sprintf (uptimestr, "%llu:%02llu:%02llu", u_hours, u_mins, u_sec); } else { sprintf (uptimestr, "%llu minute%s, %llu second%s", u_mins, (u_mins==1)?"":"s", u_sec, (u_sec==1)?"":"s"); } print_value ("Uptime","%s",uptimestr); print_value ("OS/Hardware","%s %s (%s)", VDstr("os","kernel"), VDstr("os","version"), VDstr("os","arch")); const char *dist = VDstr("os","distro"); if (dist) print_value ("Distribution", "%s", dist); Vdone("os"); /* -------------------------------------------------------------*/ print_hdr ("RESOURCES"); print_value ("Processes","\033[1m%llu\033[0m " "(\033[1m%llu\033[0m running, " "\033[1m%llu\033[0m stuck)", VDint("proc","total"), VDint("proc","run"), VDint("proc","stuck")); Vdone("proc"); print_value ("Load Average", "\033[1m%6.2f\033[0m / " "\033[1m%6.2f\033[0m / " "\033[1m%6.2f\033[0m", VAfrac ("loadavg",0), VAfrac ("loadavg", 1), VAfrac ("loadavg",2)); Vdone ("loadavg"); char cpubuf[128]; sprintf (cpubuf, "\033[1m%6.2f \033[0m%%", Vfrac("pcpu")); char meter[32]; strcpy (meter, "-[ ]+"); double iowait = VDfrac("io","pwait"); double pcpu = Vfrac("pcpu"); Vdone("pcpu"); double level = 4.5; int pos = 2; while (level < 100.0 && pos < 22) { if (level < pcpu) meter[pos++] = '#'; else meter[pos++] = ' '; level += 4.5; } print_value ("CPU", "%-40s %s", cpubuf, meter); if (iowait>0.001) print_value ("CPU iowait", "\033[1m%6.2f %%\033[0m", iowait); print_value ("Available RAM", "\033[1m%.2f\033[0m MB", ((double)VDint("mem","total"))/1024.0); print_value ("Free RAM", "\033[1m%.2f\033[0m MB", ((double)VDint("mem","free"))/1024.0); print_value ("Network in/out", "\033[1m%i\033[0m Kb/s " "(\033[1m%i\033[0m pps) / " "\033[1m%i\033[0m Kb/s " "(\033[1m%i\033[0m pps)", VDint("net","in_kbs"), VDint("net","in_pps"), VDint("net","out_kbs"), VDint("net","out_pps")); print_value ("Disk i/o", "\033[1m%i\033[0m rdops / " "\033[1m%i\033[0m wrops", VDint("io","rdops"), VDint("io","wrops")); Vdone("mem"); Vdone("net"); Vdone("io"); Vdone("badness"); print_values (apires, NULL); /* -------------------------------------------------------------*/ print_hdr ("PROCESS LIST"); const char *top_hdr[] = {"USER","PID","CPU","MEM","NAME",NULL}; const char *top_fld[] = {"user","pid","pcpu","pmem","name",NULL}; columnalign top_align[] = {CA_L, CA_R, CA_R, CA_R, CA_L, CA_NULL}; vartype top_tp[] = {VAR_STR,VAR_INT,VAR_DOUBLE,VAR_DOUBLE,VAR_STR,VAR_NULL}; int top_wid[] = {15, 7, 9, 9, 0, 0}; int top_div[] = {0, 0, 0, 0, 0, 0}; const char *top_suf[] = {"",""," %", " %", "", NULL}; var *v_top = var_get_array_forkey (apires, "top"); print_table (v_top, top_hdr, top_fld, top_align, top_tp, top_wid, top_suf, top_div); Vdone("top"); /* -------------------------------------------------------------*/ print_hdr ("STORAGE"); const char *df_hdr[] = {"DEVICE","SIZE","FS","USED","MOUNTPOINT",NULL}; const char *df_fld[] = {"device","size","fs","pused","mount",NULL}; columnalign df_aln[] = {CA_L,CA_R,CA_L,CA_R,CA_L,CA_NULL}; vartype df_tp[] = {VAR_STR,VAR_INT,VAR_STR,VAR_DOUBLE,VAR_STR,VAR_NULL}; int df_wid[] = {12, 14, 6, 8, 0}; int df_div[] = {0, (1024), 0, 0, 0, 0}; const char *df_suf[] = {""," GB", "", " %", "", ""}; var *v_df = var_get_array_forkey (apires, "df"); /*print_generic_table (v_df);*/ print_table (v_df, df_hdr, df_fld, df_aln, df_tp, df_wid, df_suf, df_div); Vdone("df"); /** Print any remaining table data */ print_tables (apires); printf ("---------------------------------------------" "-----------------------------------\n"); var_free (apires); print_done(); return 0; }
namespace Class { struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; constexpr int fn(const A &a) { return a.k; } static_assert_fold(fn(A(4,5)) == 9, ""); struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} struct C { constexpr C(C *this_) : m(42), n(this_->m) {} // ok int m, n; }; struct D { C c; constexpr D() : c(&c) {} }; static_assert_fold(D().c.n == 42, ""); struct E { constexpr E() : p(&p) {} void *p; }; constexpr const E &e1 = E(); // expected-error {{constant expression}} // This is a constant expression if we elide the copy constructor call, and // is not a constant expression if we don't! But we do, so it is. // FIXME: The move constructor is not currently implicitly defined as constexpr. // We notice this when evaluating an expression which uses it, but not when // checking its initializer. constexpr E e2 = E(); // unexpected-error {{constant expression}} static_assert_fold(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} // FIXME: We don't pass through the fact that 'this' is ::e3 when checking the // initializer of this declaration. constexpr E e3; // unexpected-error {{constant expression}} static_assert_fold(e3.p == &e3.p, ""); extern const class F f; struct F { constexpr F() : p(&f.p) {} const void *p; }; constexpr F f = F(); struct G { struct T { constexpr T(T *p) : u1(), u2(p) {} union U1 { constexpr U1() {} int a, b = 42; } u1; union U2 { constexpr U2(T *p) : c(p->u1.b) {} int c, d; } u2; } t; constexpr G() : t(&t) {} } constexpr g; static_assert_fold(g.t.u1.a == 42, ""); // expected-error {{constant expression}} static_assert_fold(g.t.u1.b == 42, ""); static_assert_fold(g.t.u2.c == 42, ""); static_assert_fold(g.t.u2.d == 42, ""); // expected-error {{constant expression}} struct S { int a, b; const S *p; double d; const char *q; constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} }; S global(43, &global); static_assert_fold(S(15, &global).b == 15, ""); constexpr bool CheckS(const S &s) { return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; } static_assert_fold(CheckS(S(27, &global)), ""); struct Arr { char arr[3]; constexpr Arr() : arr{'x', 'y', 'z'} {} }; constexpr int hash(Arr &&a) { return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; } constexpr int k = hash(Arr()); static_assert_fold(k == 0x007a7978, ""); struct AggregateInit { const char &c; int n; double d; int arr[5]; void *p; }; constexpr AggregateInit agg1 = { "hello"[0] }; static_assert_fold(strcmp_ce(&agg1.c, "hello") == 0, ""); static_assert_fold(agg1.n == 0, ""); static_assert_fold(agg1.d == 0.0, ""); static_assert_fold(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} static_assert_fold(agg1.arr[0] == 0, ""); static_assert_fold(agg1.arr[4] == 0, ""); static_assert_fold(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} static_assert_fold(agg1.p == nullptr, ""); namespace SimpleDerivedClass { struct B { constexpr B(int n) : a(n) {} int a; }; struct D : B { constexpr D(int n) : B(n) {} }; constexpr D d(3); static_assert_fold(d.a == 3, ""); } struct Bottom { constexpr Bottom() {} }; struct Base : Bottom { constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} int a; const char *b; }; struct Base2 : Bottom { constexpr Base2(const int &r) : r(r) {} int q = 123; // FIXME: When we track the global for which we are computing the initializer, // use a reference here. //const int &r; int r; }; struct Derived : Base, Base2 { constexpr Derived() : Base(76), Base2(a) {} int c = r + b[1]; }; constexpr bool operator==(const Base &a, const Base &b) { return a.a == b.a && strcmp_ce(a.b, b.b) == 0; } constexpr Base base; constexpr Base base2(76); constexpr Derived derived; static_assert_fold(derived.a == 76, ""); static_assert_fold(derived.b[2] == 's', ""); static_assert_fold(derived.c == 76 + 'e', ""); static_assert_fold(derived.q == 123, ""); static_assert_fold(derived.r == 76, ""); static_assert_fold(&derived.r == &derived.a, ""); // expected-error {{}} static_assert_fold(!(derived == base), ""); static_assert_fold(derived == base2, ""); constexpr Bottom &bot1 = (Base&)derived; constexpr Bottom &bot2 = (Base2&)derived; static_assert_fold(&bot1 != &bot2, ""); constexpr Bottom *pb1 = (Base*)&derived; constexpr Bottom *pb2 = (Base2*)&derived; static_assert_fold(pb1 != pb2, ""); static_assert_fold(pb1 == &bot1, ""); static_assert_fold(pb2 == &bot2, ""); constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} constexpr Base2 &ok2 = (Base2&)bot2; static_assert_fold(&ok2 == &derived, ""); constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} constexpr Base2 *pok2 = (Base2*)pb2; static_assert_fold(pok2 == &derived, ""); static_assert_fold(&ok2 == pok2, ""); static_assert_fold((Base2*)(Derived*)(Base*)pb1 == pok2, ""); static_assert_fold((Derived*)(Base*)pb1 == (Derived*)pok2, ""); constexpr Base *nullB = 42 - 6 * 7; static_assert_fold((Bottom*)nullB == 0, ""); static_assert_fold((Derived*)nullB == 0, ""); static_assert_fold((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); }