make torch/csrc/distributed/c10d/ProcessGroupGloo.cpp data_ptr-correct (#100689)

make torch/csrc/distributed/c10d/ProcessGroupGloo.cpp data_ptr-correct

Test Plan: Rely on CI.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100689
Approved by: https://github.com/ezyang
diff --git a/torch/csrc/distributed/c10d/ProcessGroupGloo.cpp b/torch/csrc/distributed/c10d/ProcessGroupGloo.cpp
index 19cb625..ea914a1 100644
--- a/torch/csrc/distributed/c10d/ProcessGroupGloo.cpp
+++ b/torch/csrc/distributed/c10d/ProcessGroupGloo.cpp
@@ -1100,7 +1100,7 @@
     // Construct from an existing metadata tensor to facilitate structured
     // access to metadata from peers, after gathering it.
     explicit SparseTensorMetadata(at::Tensor metadata)
-        : metadata_(metadata), data_(metadata_.data_ptr<int64_t>()) {
+        : metadata_(metadata), data_(metadata_.mutable_data_ptr<int64_t>()) {
       AT_ASSERT(metadata.scalar_type() == at::kLong);
       AT_ASSERT(metadata.dim() == 1);
       AT_ASSERT(metadata.size(0) == dim);
@@ -1237,7 +1237,7 @@
 
     // Allgather metadata
     gloo::AllgatherOptions opts(context);
-    opts.setOutput(buffer.data_ptr<int64_t>(), buffer.numel());
+    opts.setOutput(buffer.mutable_data_ptr<int64_t>(), buffer.numel());
     opts.setTag(tag);
     gloo::allgather(opts);
 
@@ -1264,8 +1264,9 @@
 
     // Allgatherv indices.
     gloo::AllgathervOptions opts(context);
-    opts.setInput(input.data_ptr<int64_t>(), input.numel());
-    opts.setOutput(output.data_ptr<int64_t>(), counts);
+    opts.setInput(
+        const_cast<int64_t*>(input.const_data_ptr<int64_t>()), input.numel());
+    opts.setOutput(output.mutable_data_ptr<int64_t>(), counts);
     opts.setTag(tag);
     gloo::allgatherv(opts);
 
@@ -2622,12 +2623,12 @@
     int tag) {
   auto& tensor = checkSingleTensor(tensors);
   auto utag = checkTag(tag);
-  auto ptr = tensor.data_ptr();
+  auto ptr = tensor.const_data_ptr();
   auto size = tensor.numel() * tensor.element_size();
 
   // Construct unbound buffer.
   auto context = getContext(tag);
-  auto buf = context->createUnboundBuffer(ptr, size);
+  auto buf = context->createUnboundBuffer(const_cast<void*>(ptr), size);
   buf->send(dstRank, utag);
 
   // The work captures the tensor to prevent it being deallocated and
@@ -2641,7 +2642,7 @@
     int tag) {
   auto& tensor = checkSingleTensor(tensors);
   auto utag = checkTag(tag);
-  auto ptr = tensor.data_ptr();
+  auto ptr = tensor.mutable_data_ptr();
   auto size = tensor.numel() * tensor.element_size();
 
   // Construct unbound buffer.
@@ -2659,7 +2660,7 @@
     int tag) {
   auto& tensor = checkSingleTensor(tensors);
   auto utag = checkTag(tag);
-  auto ptr = tensor.data_ptr();
+  auto ptr = tensor.mutable_data_ptr();
   auto size = tensor.numel() * tensor.element_size();
 
   // Construct unbound buffer.