aboutsummaryrefslogtreecommitdiff
path: root/src/Panda/Attribute.C
diff options
context:
space:
mode:
Diffstat (limited to 'src/Panda/Attribute.C')
-rw-r--r--src/Panda/Attribute.C187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/Panda/Attribute.C b/src/Panda/Attribute.C
new file mode 100644
index 0000000..0c50f04
--- /dev/null
+++ b/src/Panda/Attribute.C
@@ -0,0 +1,187 @@
+#include "definitions.h"
+#include "Attribute.h"
+#include "MPIFS.h"
+#include "string.h"
+
+
+extern MPIFS *MPIFS_global_obj;
+extern "C" {
+ int IOwriteAttribute(IOFile,char*,int,int,void *);
+ int IOsizeOf(int);
+ int IOreadAttributeInfo(IOFile,char*,int*,int*);
+ int IOreadAttribute(IOFile,int,void*);
+// IOFile IEEEopen(char *,char *);
+}
+
+Attribute::Attribute()
+{
+ name_ = NULL;
+ data_status_ = 0;
+ data_ = NULL;
+}
+
+void Attribute::init(char *name)
+{
+ int len = strlen(name);
+ name_ = (char *)malloc(sizeof(char) * (len + 1));
+ for (int i=0; i<len; i++) name_[i] = name[i];
+ name_[i] = '\0';
+}
+
+void Attribute::init(char *name, int esize, int count, void *data)
+{
+ int len = strlen(name);
+ name_ = (char *)malloc(sizeof(char) * (len + 1));
+ for (int i=0; i<len; i++) name_[i] = name[i];
+ name_[i] = '\0';
+ esize_ = esize;
+ count_ = count;
+ data_ = data;
+ data_status_ = 0;
+}
+
+Attribute::~Attribute()
+{
+ if (name_) free(name_);
+ if (data_status_ && data_) free(data_);
+}
+
+void Attribute::pack(int &schema_len, char *&schema, char *fname, int op_type)
+{
+ union int_to_char tmp;
+ int i, real_size = IOsizeOf(esize_);
+
+ int len1 = strlen(fname);
+ int len = strlen(name_);
+ if (op_type == TIMESTEP)
+ schema_len = 5 * sizeof(int) + len1 + len + real_size * count_;
+ else schema_len = 3 * sizeof(int) + len1 + len;
+ schema = (char *)malloc(sizeof(char) * schema_len);
+ char *ptr = schema;
+
+ tmp.i = op_type;
+ for (i=0; i<4; i++) *ptr++ = tmp.c[i];
+ tmp.i = len1;
+ for (i=0; i<4; i++) *ptr++ = tmp.c[i];
+ for (i=0; i<len1; i++) *ptr++ = fname[i];
+ tmp.i = len;
+ for (i=0; i<4; i++) *ptr++ = tmp.c[i];
+ for (i=0; i<len; i++) *ptr++ = name_[i];
+
+ if (op_type == TIMESTEP) {
+ tmp.i = esize_;
+ for (i=0; i<4; i++) *ptr++ = tmp.c[i];
+ tmp.i = count_;
+ for (i=0; i<4; i++) *ptr++ = tmp.c[i];
+ memcpy(ptr, data_, real_size * count_);
+ }
+}
+
+Attribute::Attribute(char *schema, int op_type)
+{
+ union int_to_char tmp;
+ int i, len, real_size;
+ char *ptr = schema;
+
+ for (i=0; i<4; i++) tmp.c[i] = *ptr++;
+ len = tmp.i;
+ name_ = (char *)malloc(sizeof(char) * (len + 1));
+ for (i=0; i<len; i++) name_[i] = *ptr++;
+ name_[i] = '\0';
+
+ if (op_type == TIMESTEP) {
+ for (i=0; i<4; i++) tmp.c[i] = *ptr++;
+ esize_ = tmp.i;
+ real_size = IOsizeOf(esize_);
+ for (i=0; i<4; i++) tmp.c[i] = *ptr++;
+ count_ = tmp.i;
+ data_ = (void *)malloc(esize_ * count_);
+ memcpy(data_, ptr, real_size * count_);
+ data_status_ = 1;
+ }
+}
+
+void Attribute::read(char *fname, char *n)
+{
+ int node_type = MPIFS_global_obj->node_type();
+ IOFile fp;
+
+ if (node_type == PART_TIME_COMPUTE || node_type == COMPUTE_NODE) {
+ if (MPIFS_global_obj->am_master_compute_node()) {
+ init(n);
+ MPIFS_global_obj->send_attr_schema(this, fname, READ_TIMESTEP);
+ }
+ MPIFS_global_obj->receive_attr_data(this);
+ } else { // PART_TIME_IO
+ init(n);
+ if (MPIFS_global_obj->am_master_compute_node())
+ MPIFS_global_obj->send_attr_schema(this, fname, READ_TIMESTEP);
+ MPIFS_global_obj->receive_attr_schema();
+
+ int len = strlen(fname);
+ char *name = (char *)malloc(sizeof(char) * (len+1));
+ char *name1 = (char *)malloc(sizeof(char) * (len+6));
+ for (int i=0; i<len; i++) name[i] = fname[i];
+ name[i] = '\0';
+ sprintf(name1, "%s.%d", name, MPIFS_global_obj->my_rank(IO_NODE));
+ fp = MPIFS_global_obj->open_file(name1, READ_TIMESTEP);
+ read_data(fp);
+ if (MPIFS_global_obj->am_master_io_node()) {
+ MPIFS_global_obj->send_attr_data(this);
+ }
+ MPIFS_global_obj->receive_attr_data(this);
+ free(name);
+ }
+}
+
+void Attribute::write(char *fname, char *n, int esize, int count, void *data)
+{
+ int node_type = MPIFS_global_obj->node_type();
+
+ if (node_type == PART_TIME_COMPUTE || node_type == COMPUTE_NODE) {
+ if (MPIFS_global_obj->am_master_compute_node()) {
+ init(n, esize, count, data);
+ MPIFS_global_obj->send_attr_schema(this, fname, TIMESTEP);
+ }
+ } else { // PART_TIME_IO
+ init(n, esize, count, data);
+ if (MPIFS_global_obj->am_master_compute_node())
+ MPIFS_global_obj->send_attr_schema(this, fname, TIMESTEP);
+ MPIFS_global_obj->receive_attr_schema();
+
+ IOFile fp;
+ int len = strlen(fname);
+ char *name = (char *)malloc(sizeof(char) * (len+1));
+ char *name1 = (char *)malloc(sizeof(char) * (len+6));
+ for (int i=0; i<len; i++) name[i] = fname[i];
+ name[i] = '\0';
+ sprintf(name1, "%s.%d", name, MPIFS_global_obj->my_rank(IO_NODE));
+
+ fp = MPIFS_global_obj->open_file(name1, TIMESTEP);
+
+ write_data(fp);
+ free(name);
+ }
+}
+
+void Attribute::write_data(IOFile fp)
+{
+ IOwriteAttribute(fp, name_, esize_, count_, data_);
+}
+
+void Attribute::read_data(IOFile fp)
+{
+ int index = IOreadAttributeInfo(fp, name_, &esize_, &count_);
+ if (index >= 0) {
+ data_ = (void *)malloc(IOsizeOf(esize_) * count_);
+ IOreadAttribute(fp, index, data_);
+ } else printf("Fail to read attribute %s\n", name_);
+}
+
+void *Attribute::get_data_ptr() { return data_; }
+void Attribute::set_data_ptr(void *d) { data_ = d; }
+int Attribute::data_size() { return IOsizeOf(esize_) * count_; }
+int Attribute::esize() { return esize_; }
+int Attribute::count() { return count_; }
+void Attribute::set_count(int c) { count_ = c; }
+void Attribute::set_esize(int e) { esize_ = e; }