From: <spa...@us...> - 2008-12-11 04:03:29
|
Revision: 7220 http://x10.svn.sourceforge.net/x10/?rev=7220&view=rev Author: sparksparkspark Date: 2008-12-11 04:03:20 +0000 (Thu, 11 Dec 2008) Log Message: ----------- changed primitive toString() to use snprintf added x10aux::main which is templated over "Runtime" (one should usually give it x10::runtime::Runtime) and "T" which is the class containing the static main method we will invoke. Uncommented some code from StringBuilder that ought to now work (code gen bug fixed some time ago) Refactored NQueensPar to not depend on DistUtil Removed (broken) use of printf from FRASimpleDist.x10 (native printf does not understand boxes) Modified Paths: -------------- trunk/x10.runtime.17/src-cpp/Makefile trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.cc trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.h trunk/x10.runtime.17/src-cpp/x10aux/bootstrap.h trunk/x10.runtime.17/src-cpp/x10aux/config.h trunk/x10.runtime.17/src-cpp/x10aux/string_utils.cc trunk/x10.runtime.17/src-cpp/x10aux/string_utils.h trunk/x10.runtime.17/src-x10/x10/lang/Rail.x10 trunk/x10.runtime.17/src-x10/x10/util/StringBuilder.x10 trunk/x10.tests/examples/Misc/FRASimpleDist.x10 trunk/x10.tests/examples/Misc/NQueensPar.x10 Modified: trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.cc =================================================================== --- trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.cc 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.cc 2008-12-11 04:03:20 UTC (rev 7220) @@ -1,4 +1,6 @@ +#include <x10aux/config.h> #include <x10aux/boolean_utils.h> +#include <x10aux/string_utils.h> #include <x10/lang/String.h> \ @@ -11,11 +13,8 @@ using namespace std; using namespace x10aux; -ref<String> x10aux::boolean_utils::_trueString = String::Lit("true"); -ref<String> x10aux::boolean_utils::_falseString = String::Lit("false"); - const ref<String> x10aux::boolean_utils::toString(x10_boolean value) { - return value ? _trueString : _falseString; + return x10aux::to_string(value); } x10_boolean x10aux::boolean_utils::parseBoolean(const ref<String>& s) { Modified: trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.h =================================================================== --- trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.h 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-cpp/x10aux/boolean_utils.h 2008-12-11 04:03:20 UTC (rev 7220) @@ -17,9 +17,6 @@ static const ref<x10::lang::String> toString(x10_boolean value); static x10_boolean parseBoolean(const ref<x10::lang::String>& s); - private: - static ref<x10::lang::String> _trueString; - static ref<x10::lang::String> _falseString; }; } Modified: trunk/x10.runtime.17/src-cpp/x10aux/bootstrap.h =================================================================== --- trunk/x10.runtime.17/src-cpp/x10aux/bootstrap.h 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-cpp/x10aux/bootstrap.h 2008-12-11 04:03:20 UTC (rev 7220) @@ -2,14 +2,24 @@ #define X10AUX_BOOTSTRAP_H #include <x10aux/config.h> +#include <x10aux/pgas.h> +#include <x10aux/alloc.h> +#include <x10aux/string_utils.h> +#include <x10aux/init_dispatcher.h> #include <x10/lang/VoidFun_0_0.h> #include <x10/lang/String.h> +#include <x10/lang/Rail.h> +#include <x10/lang/Iterator.h> +#include <x10/runtime/Thread.h> + namespace x10 { namespace lang { template<class T> class Rail; } } namespace x10aux { + extern x10_int exitCode; + typedef void (*ApplicationMainFunction)(ref<x10::lang::Rail<ref<x10::lang::String> > >); class BootStrapClosure : public x10::lang::Value, @@ -40,8 +50,85 @@ }; + template<class Runtime, class T> int main(int ac, char **av) { + + x10aux::ref<x10::lang::Rail<x10aux::ref<x10::lang::String> > > args = + x10aux::convert_args(ac, av); - extern x10_int exitCode; +#ifndef NO_EXCEPTIONS + try { +#endif + x10aux::barrier(); + + // Initialise enough state to make this 'main' thread look like a normal x10 thread + // (e.g. make Thread::CurrentThread work properly). + x10::runtime::Thread::_make(x10aux::null, x10::lang::String::Lit("thread-main")); + + // Initialise the static fields of x10 classes. + x10aux::InitDispatcher::runInitializers(); + + // Construct closure to invoke the user's "public static def main(Rail[String]) : Void" + // if at place 0 otherwise wait for asyncs. + x10aux::ref<x10::lang::VoidFun_0_0> main_closure = + new (x10aux::alloc<x10::lang::VoidFun_0_0>(sizeof(x10aux::BootStrapClosure))) + x10aux::BootStrapClosure(T::main,args); + + Runtime::start(main_closure); // use XRX + //main_closure->apply(); // bypass XRX + //sleep(3); + +#ifndef NO_EXCEPTIONS + } catch(int exitCode) { + + x10aux::exitCode = exitCode; + + } catch(x10aux::__ref& e) { + + // Assume that only throwables can be thrown + // and things are never thrown by interface (always cast to a value/object class) + x10aux::ref<x10::lang::Throwable> &e_ = + static_cast<x10aux::ref<x10::lang::Throwable>&>(e); + + fprintf(stderr, "Uncaught exception at place %d of type: %s\n", + (int)x10_here(), e_->_type()->name().c_str()); + fprintf(stderr, "%s\n", e_->toString()->c_str()); + + x10aux::ref<x10::lang::ValRail<x10aux::ref<x10::lang::String> > > trace = + e_->getStackTrace(); + + x10aux::ref<x10::lang::Iterator<x10aux::ref<x10::lang::String> > > it = + trace->iterator(); + + while (it->hasNext()) { + fprintf(stderr, " at %s\n", it->next()->c_str()); + } + + x10aux::exitCode = 1; + + } catch(...) { + + fprintf(stderr, "Caught unrecognised exception at place %d\n", (int)x10_here()); + x10aux::exitCode = 1; + + } +#endif + + //fprintf(stderr, "Done with main in place %d", (int)x10_here()); + + x10aux::free_args(args); + + x10aux::shutdown(); + + if (getenv("X10_RXTX")!=NULL) + fprintf(stderr, "Place: %d rx: %lld tx: %lld\n", + (int)x10_here(), + (long long)x10aux::deserialized_bytes, + (long long)x10aux::serialized_bytes); + + return x10aux::exitCode; + } + + } #endif Modified: trunk/x10.runtime.17/src-cpp/x10aux/config.h =================================================================== --- trunk/x10.runtime.17/src-cpp/x10aux/config.h 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-cpp/x10aux/config.h 2008-12-11 04:03:20 UTC (rev 7220) @@ -161,6 +161,7 @@ typedef float x10_float; typedef double x10_double; + // We must use the same mangling rules as the compiler backend uses. // The c++ target has to mangle fields because c++ does not allow fields // and methods to have the same name. Modified: trunk/x10.runtime.17/src-cpp/x10aux/string_utils.cc =================================================================== --- trunk/x10.runtime.17/src-cpp/x10aux/string_utils.cc 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-cpp/x10aux/string_utils.cc 2008-12-11 04:03:20 UTC (rev 7220) @@ -1,3 +1,6 @@ +#include <cmath> + +#include <x10aux/config.h> #include <x10aux/string_utils.h> #include <x10aux/rail_utils.h> #include <x10aux/alloc.h> @@ -27,7 +30,9 @@ //cerr << "free_args: freed array " << arr << endl; } + // [DC] I'm sure Igor will hate this but it will do for now. +/* template<class T> T x10aux::from_string(const ref<String> &s) { std::istringstream ss(*s); T x; @@ -36,7 +41,9 @@ } return x; } +*/ + // [DC] I'm sure Igor will hate this but it will do for now. template<class T> String to_string_general(T v) { std::ostringstream ss; @@ -46,45 +53,96 @@ return r; } -String x10aux::to_string(x10_boolean v) { +#define TO_STRING(SZ,T,FMT) \ +String x10aux::to_string(T v) { \ + char buf[SZ]; \ + int amt = ::snprintf(buf, sizeof buf, FMT, v); \ + assert((size_t)amt<sizeof buf && "buf too small "__TOKEN_STRING(SZ)" for "__TOKEN_STRING(T)); \ + String r; \ + r._constructor(buf); \ + return r; \ +} + +// hh is C99, not ansi c, so we use h instead. +// This is fine as va_args turns everything to int anyway +TO_STRING(4, unsigned char, "%hu") +TO_STRING(5, signed char, "%hd") + +//TO_STRING(6, unsigned short, "%hu") +TO_STRING(7, signed short, "%hd") + +TO_STRING(11, unsigned int, "%u") +TO_STRING(12, signed int, "%d") +TO_STRING(11, unsigned long, "%lu") +TO_STRING(12, signed long, "%ld") +TO_STRING(20, unsigned long long, "%llu") +TO_STRING(21, signed long long, "%lld") + +String x10aux::to_string(float v) { + return x10aux::to_string((double)v); +} + +// precondition: buf contains decimal point +void kill_excess_zeroes(char *buf, size_t sz) { + for(int i=sz-1 ; i>0 && (buf[i]=='0' || buf[i]=='\0') ; --i) { + if (buf[i-1]=='.') break; + buf[i] = '\0'; + } +} + + +String x10aux::to_string(double v) { + char buf[120] = ""; + if (::isnan(v)) { + ::snprintf(buf, sizeof buf, "NaN"); + } else if (v==INFINITY) { + ::snprintf(buf, sizeof buf, "Infinity"); + } else if (v==-INFINITY) { + ::snprintf(buf, sizeof buf, "-Infinity"); + } else if (fabs(v)>=1E-3 && fabs(v)<1E7) { + ::snprintf(buf, sizeof buf, "%.15f", v); + kill_excess_zeroes(buf, sizeof buf); + } else if (v==0) { + ::snprintf(buf, sizeof buf, "%.1f", v); + } else { + // scientific notation + int e = ::floor(::log(::fabs(v))/::log(10)); //exponent + // volatile because reordering could change computed floating point value + volatile double m = v / pow(10,e); //mantissa + if (e<-10) { + // avoid touching -Infinity + m = v * 1E10; + m /= pow(10,e+10); + } + if (e<0) { + ::snprintf(buf, sizeof buf, "%.1f", m); + } else { + ::snprintf(buf, sizeof buf, "%.16f", m); + } + kill_excess_zeroes(buf, sizeof buf); + char *rest = buf + strlen(buf); + ::snprintf(rest, sizeof(buf) + buf - rest, "E%d", e); + } String r; - r._constructor(v?"true":"false"); + r._constructor(buf); return r; } -String x10aux::to_string(x10_byte v) { - return to_string_general((int)v); +String x10aux::to_string(bool v) { + String r; + r._constructor(v?"true":"false"); + return r; } String x10aux::to_string(x10_char v) { - return to_string_general((char)v); + char v_[] = {(char)v,'\0'}; + String r; + r._constructor(v_); + return r; } - -String x10aux::to_string(x10_short v) { - return to_string_general((short)v); -} - -String x10aux::to_string(x10_int v) { - return to_string_general((int)v); -} - -String x10aux::to_string(x10_long v) { - return to_string_general((int64_t)v); -} - -String x10aux::to_string(x10_float v) { - return to_string_general((float)v); -} -String x10aux::to_string(x10_double v) { - return to_string_general((double)v); -} -String x10aux::to_string(const char *v) { - return to_string_general(std::string(v)); -} - -String x10aux::to_string(ref<ValRail<x10_char> > v) { +String x10aux::vrc_to_string(ref<ValRail<x10_char> > v) { std::string str(v->FMGL(length), '\0'); for (int i = 0; i < v->FMGL(length); ++i) str[i] = (*v)[i]; Modified: trunk/x10.runtime.17/src-cpp/x10aux/string_utils.h =================================================================== --- trunk/x10.runtime.17/src-cpp/x10aux/string_utils.h 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-cpp/x10aux/string_utils.h 2008-12-11 04:03:20 UTC (rev 7220) @@ -18,27 +18,39 @@ void free_args(const ref<x10::lang::Rail<ref<x10::lang::String> > > &arr); - // these need to use the x10_ primitive typedefs, because otherwise they - // are not found. [DC] found by whom and by what mechanism? overload - // resolution respects typedefs... + // [DC] importing the old code requires me to put these back to what they + // were. Please add more options if types are not found, but do not use + // the x10_ typedefs since this will break the implementation in unpleasant + // ways. - x10::lang::String to_string(x10_boolean v); - x10::lang::String to_string(x10_byte v); + x10::lang::String to_string(bool v); + x10::lang::String to_string(unsigned char v); + x10::lang::String to_string(signed char v); + x10::lang::String to_string(unsigned short v); + x10::lang::String to_string(signed short v); + x10::lang::String to_string(unsigned int v); + x10::lang::String to_string(signed int v); + x10::lang::String to_string(unsigned long v); + x10::lang::String to_string(signed long v); + x10::lang::String to_string(unsigned long long v); + x10::lang::String to_string(signed long long v); + + x10::lang::String to_string(float v); + x10::lang::String to_string(double v); + + // special case -- we want a static error if it conflicts with any of the above x10::lang::String to_string(x10_char v); - x10::lang::String to_string(x10_short v); - x10::lang::String to_string(x10_int v); - x10::lang::String to_string(x10_long v); - x10::lang::String to_string(x10_float v); - x10::lang::String to_string(x10_double v); - x10::lang::String to_string(const char *v); - x10::lang::String to_string(x10aux::ref<x10::lang::ValRail<x10_char> > v); + // Used by x10/util/StringBuilder.x10 + x10::lang::String vrc_to_string(x10aux::ref<x10::lang::ValRail<x10_char> > v); +/* template<class T> ref<x10::lang::String> to_stringp(T v) { return to_string(v); } +*/ - template<class T> T from_string(const ref<x10::lang::String> &s); + //template<class T> T from_string(const ref<x10::lang::String> &s); } Modified: trunk/x10.runtime.17/src-x10/x10/lang/Rail.x10 =================================================================== --- trunk/x10.runtime.17/src-x10/x10/lang/Rail.x10 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-x10/x10/lang/Rail.x10 2008-12-11 04:03:20 UTC (rev 7220) @@ -37,11 +37,11 @@ public native static def makeVar[S](length: nat): Rail[S]{self.length==length}; @Native("java", "x10.core.RailFactory.<#2>makeRailFromValRail(#3, #4)") - @Native("c++", "x10::lang::Rail<#2 >::make(#4)") + @Native("c++", "x10::lang::Rail<#1 >::make(#4)") public native static def make[U](r: ValRail[U]): Rail[U]{self.length==r.length}; @Native("java", "x10.core.RailFactory.<#2>makeRailFromValRail(#3, #4)") - @Native("c++", "x10::lang::Rail<#2 >::make(#4)") + @Native("c++", "x10::lang::Rail<#1 >::make(#4)") public native static def $convert[U](r: ValRail[U]): Rail[U]{self.length==r.length}; @Native("java", "#0.get(#1)") Modified: trunk/x10.runtime.17/src-x10/x10/util/StringBuilder.x10 =================================================================== --- trunk/x10.runtime.17/src-x10/x10/util/StringBuilder.x10 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.runtime.17/src-x10/x10/util/StringBuilder.x10 2008-12-11 04:03:20 UTC (rev 7220) @@ -17,16 +17,9 @@ } protected def addString(s: String): Builder[Object,String] { - for (var i: int = 0; i < s.length(); i++) { - val ch = s(i); - buf.add(ch); - } - // BROKEN code gen - /* for (ch in s.chars()) { buf.add(ch); } - */ return this; } @@ -35,7 +28,7 @@ } @Native("java", "new String(#1.getCharArray())") - @Native("c++", "x10aux::to_string(#1)") + @Native("c++", "x10aux::vrc_to_string(#1)") private static native def makeString(ValRail[Char]): String; public def result(): String { Modified: trunk/x10.tests/examples/Misc/FRASimpleDist.x10 =================================================================== --- trunk/x10.tests/examples/Misc/FRASimpleDist.x10 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.tests/examples/Misc/FRASimpleDist.x10 2008-12-11 04:03:20 UTC (rev 7220) @@ -118,8 +118,8 @@ // print statistics val GUPs = (cpuTime > 0.0 ? 1.0 / cpuTime : -1.0) * NUM_UPDATES / 1e9; - printf("CPU time used = %.2f seconds\n", cpuTime); - printf("%.6f Billion(10^9) Updates per second (GUP/s)\n", GUPs); + println("CPU time used = "+cpuTime+" seconds"); + println(GUPs+" Billion(10^9) Updates per second (GUP/s)"); // repeat for testing. randomAccessUpdate(NUM_UPDATES, logLocalTableSize, tables); @@ -138,7 +138,4 @@ static def println(s:String) = Console.OUT.println(s); - @Native("java", "System.out.printf(#1,#2)") - @Native("c++", "printf((#1)->c_str(), #2); fflush(stdout)") - public static native def printf(x:String, o:Object):void; } Modified: trunk/x10.tests/examples/Misc/NQueensPar.x10 =================================================================== --- trunk/x10.tests/examples/Misc/NQueensPar.x10 2008-12-10 19:15:51 UTC (rev 7219) +++ trunk/x10.tests/examples/Misc/NQueensPar.x10 2008-12-11 04:03:20 UTC (rev 7220) @@ -15,6 +15,16 @@ new Board().search(); } + public static def block(R: Region(1), P:Int):ValRail[Region(1)](P) = { + assert P >=0; + val low = R.min()(0), high = R.max()(0), count = high-low+1; + val baseSize = count/P, extra = count - baseSize*P; + ValRail.make[Region(1)](P, (i:Nat):Region(1) => { + val start = low+i*baseSize+ (i < extra? i:extra); + start..start+baseSize+(i < extra?0:-1) + }) + } + class Board { val q: Rail[Int]; @@ -52,7 +62,7 @@ return; } if (q.length==0) { - val R = DistUtil.block(0..N-1, P); + val R = block(0..N-1, P); //foreach ((q) in 0..P-1) // search(R(q)); for (var q:Int=0; q < P; ++q) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |